zoukankan      html  css  js  c++  java
  • [转载]正则表达式学习

    原文地址:正则表达式学习作者:理想


    使用grep搜索字符串:

    特定字符串:grep -n 'string'  file
    搜索集合字符串:grep -n 't[abc]'  file  搜索包含ta/tb/tc的字符串
    利用反向选择搜索:grep -n 'p[^r]' file  搜索file中p后面不为r的字符串
    grep -n '[^a-z]str'  file 或者 grep -n '[^[:lower:]]str' :file 中str前不为小写字母的字符串
    [:lower:]=a-z;[[:lower:]]=[a-z]
    [:digit:]=0-9;[[:digit:]]=[0-9]
    [:upper:]=A-Z;[[:upper:]]=[A-Z]
    行首与行尾:   行首:^ 行尾:$
    grep '^[a-z]' file :搜索file中以小写字母开头的行。
    grep '^[^a-zA-Z]'  file:file中不以字母开头的行
    注意:^放在括号内表示排除,放在括号外表示行首
    '^[a-z]'与'[^a-z]'的区别:
    '^[a-z]'表示以小写字母开头,
    '[^a-z]'表示不包含小写字母
    搜索以数字结尾的行:grep -n '[0-9]$' file
    搜索以句号结尾的行: grep -n '.$' file
    查找空白行:grep -n '^$' file
    查找非空白行:grep -vn '^$' file

    任意字符与重复字符
    .(点)代表任一字符;*(星花)代表重复前1字符1到n次
    [root@localhost proc]# grep -n 'p....f' tmp.txt
    7:printf lslslsllsls

    [root@localhost proc]# grep -n 'ab*' tmp.txt
    1:abcdef aaaaaaaaaaaaaaaaaa
    2:abcdef aaaaaaaaaaaaaaaaaa
    3:abcdef aaaaaaaaaaaaaaaaaa
    4:abcdef aaaaaaaaaaaaaaaaaa
    5:abcdef aaaaaaaaaaaaaaaaaa
    6:abcdef aaaaaaaaaaaaaaaaaa
    8:abcd
    [root@localhost proc]# grep -n 'aaa*' tmp.txt
    1:abcdef aaaaaaaaaaaaaaaaaa
    2:abcdef aaaaaaaaaaaaaaaaaa
    3:abcdef aaaaaaaaaaaaaaaaaa
    4:abcdef aaaaaaaaaaaaaaaaaa
    5:abcdef aaaaaaaaaaaaaaaaaa
    6:abcdef aaaaaaaaaaaaaaaaaa
    查找以p开头以s结尾的行:
    grep -n 'p.*s' tmp.txt
    grep -n '[^a-zA-Z0-9]str'  file:file中str前既不是小写字母,也不是大写字母也不是数字的字符串

    设置字符出现次数:
    grep -n 'a{2}' tmp.txt:tmp.txt中重复出现2个a的字符串
    grep -n 'a{2,5}g' file:file中a出现2-5次后面接g的字符串

  • 相关阅读:
    linux查看用户组所有成员
    navicat for mysql 在Mac上安装后没有连接列表,就是左边的那一列连接项目怎么办?
    mysql启动问题access denied for user 'root'@'localhost'(using password:YES)
    phpcms多站点表单统一到主站点管理的解决方案
    thinkphp5.0 session驱动方式问题汇总
    Python__开启进程的两种方式
    Python并发编程之操作系统理论部分
    操作系统简介
    Python__基于udp的套接字
    网络编程1
  • 原文地址:https://www.cnblogs.com/big4panda/p/6417574.html
Copyright © 2011-2022 走看看