zoukankan      html  css  js  c++  java
  • 正则表达式(1):入门

    正则表达式,也叫规则表达式

    通过规则表达式找到我们想要的东西


    ^ 锚定行首

    [root@tz shell]# cat regex
    hello world
    hi	hello
    hello ,zsy
    [root@tz shell]# grep "^hello" regex
    hello world
    hello ,zsy
    


    $ 锚定行尾

    [root@tz shell]# grep "hello$" regex
    hi	hello
    

    ^xxx$ 结合使用,既位于行首也位于行尾,即整行只有xxx

    参数-n显示行号,参数color显示颜色

    [root@tz shell]# cat regex
    hello world
    hi	hello
    hello ,zsy
    hello
    [root@tz shell]# grep -n --color "^hello$" regex
    4:hello
    

    ^$ 匹配空行

    [root@tz shell]# grep -n "^$" regex
    3:
    


    < 锚定词首

    > 锚定词尾

    [root@tz shell]# cat REG
    abchello world #可以看到hello位于abchello词尾
    abc helloabc abc
    abc abchelloabc abc
    [root@tz shell]# grep --color "<hello" REG
    abc helloabc abc
    [root@tz shell]# grep --color "hello>" REG
    abchello world
    

    <与>  结合

    [root@tz shell]# cat REG
    abchello world
    abc helloabc abc
    abc abchelloabc abc
    hello
    [root@tz shell]# grep --color "<hello>" REG
    hello
    

    <与>  结合使用 ,即既是行首也是行尾,即单独的字符串,可以用代替


    锚定词首

    [root@tz shell]# grep --color "hello" REG
    abc helloabc abc
    hello
    [root@tz shell]# grep --color "hello" REG
    abchello world
    hello
    [root@tz shell]# grep --color "hello" REG
    hello
    


    B 锚定非词首

    只要词首不是hello都会被匹配

    [root@tz shell]# grep --color "Bhello" REG
    abchello world
    abc abchelloabc abc
    abchello helloabc hello ahelloa
    



    学习来自朱双印朱老师的博客

    今天的学习是为了以后的工作更加的轻松!
  • 相关阅读:
    KISSY 1.3.0 发布,淘宝 Web UI 库
    mongodb的监控与性能优化
    Aspose.Tasks 4.9.0 发布,Project 文件读写
    Fix8 0.6.6 发布,C++ 实现的 FIX 框架
    VIM Pal 1.1.0 发布,VIM 文件树列表
    QT 5.0 正式版发布,支持 C++11
    TWiki 5.1.3 发布,企业 Wiki 系统
    Apache Lucene 3.6.2 发布
    Apache Sqoop 1.99.1 发布
    JAXX 2.5.9 发布,XML用户界面框架
  • 原文地址:https://www.cnblogs.com/tz90/p/12824665.html
Copyright © 2011-2022 走看看