• 文本三剑客--前戏


    正则表达式

    基础正则表达式:BRE(basic regular express)

    扩展正则表达式:ERE(extend regular express),扩展的表达式有+、?、|和() 

    符号 描述    范例  
    . 出 匹配任意一个单字符

    [root@localhost ~]# echo -e "123444 xf"|grep "12."
    123444

    ^ 匹配字符串开头的

    [root@localhost ~]# echo -e "123444 xf"|grep "^12"
    123444
    [root@localhost ~]# echo -e "123444 xf"|grep "^x"
    xf

    $ 匹配前面字符串结尾

    [root@localhost ~]# echo -e "123444 xf"|grep "f$"
    xf
    [root@localhost ~]# echo -e "123444 xf"|grep "44$"
    123444

    * 匹配前面字符0个或多个

    [root@localhost ~]# echo -e "xo xooo xoghjk"|grep "o*"
    xo
    xooo
    xoghjk

    + 匹配前面一个或多个

    [root@localhost ~]# echo -e "abc abbbbcc add" |grep -E 'ab+'
    abc
    abbbbcc

    匹配前字符0或1个

    [root@localhost ~]# echo -e "ac abbbc addd"|grep -E "a?c"
    ac
    abbbc

    [] 匹配任意一个字符

    [root@localhost ~]# echo -e "ac ad db"|grep "[ac]"
    ac
    ad

    [.-.] 匹配括号中任意字符 [0-9]  [a-z] 
    [^] 匹配除括号内的字符

    [root@localhost ~]# echo -e "ac ad db"|grep "[^ac]"
    ad
    db

    {n} 至少匹配n次前面的字符

    [root@localhost ~]# echo -e "a abc c" |grep -E '[a-z]{3,}'
    abc

    {n,m} 匹配n次-m次

    [root@localhost ~]# echo -e "a abc cd" |grep -E '[a-z]{1,3}'
    a
    abc
    cd

     ()  单元或组合,就是一个整体  

    [root@localhost ~]# echo "123abc" |grep -E -o '(abc)'
    abc

     < 以字符开始   

    [root@localhost ~]# echo -e "1 12 123 1234" |grep '<123'
    123
    1234

     >  以字符结束  

    [root@localhost ~]# echo -e "1 12 123 1234" |grep '4>'
    1234

     |  匹配两边的任意一个  

    [root@localhost ~]# echo -e "123 abc" |grep -o -E "(123|abc)"
    123
    abc

  • 相关阅读:
    学生排队 201703-2
    让动画停在最后一帧 forwards animation-fill-mode
    新版本的charles代理本地接口
    移动端调试插件Tencent / vConsole
    HDU
    Codeforces Round #668 (Div. 2)(A B C D)
    The 13th Chinese Northeast Collegiate Programming Contest
    2020, XIII Samara Regional Intercollegiate Programming Contest (B D)
    tarjan算法 双连通分量
    Codeforces Round #666 (Div. 2) (A B C D)
  • 原文地址:https://www.cnblogs.com/linux-error/p/11172441.html
Copyright © 2020-2023  润新知