• 正则表达式(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
    



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

    今天的学习是为了以后的工作更加的轻松!
  • 相关阅读:
    触摸屏与usb鼠标同时支持
    QT国际化(lupdate/linguits/lrelease)生成.ts,转换成.qm方法
    Qt5 使用lambda
    c++中lambda表达式的用法
    异或运算的作用
    函数指针和指针函数用法和区别
    前端html页面学习---html部分
    二:maven构建module
    一:使用maven构建项目
    maven项目发布到tomcat后没有lib目录解决方案
  • 原文地址:https://www.cnblogs.com/tz90/p/12824665.html
Copyright © 2020-2023  润新知