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的字符串

  • 相关阅读:
    css浮动布局小技巧
    dhtml
    js 图片轮播
    css 简单梯形
    css 平行四边
    activity添加切换动画之后出现的黑色背景问题
    Android 设置ImageView宽度固定,其高度按比例缩放适应
    白底黑字!Android浅色状态栏黑色字体模式(另)
    Adroid ViewPage+GridView实现每页6个元素,三页滑动切换
    Android View转换成图片保存
  • 原文地址:https://www.cnblogs.com/big4panda/p/6417574.html
Copyright © 2011-2022 走看看