• 正则表达式基础->


    描述:(grep)

    正则表达式是一种字符模式,用于在查找过程中匹配指定的字符。在大多数程序里,正则表达式都被置于两个正斜杠之间,它匹配被查找的行中任何位置出现的相同模式

    基础正则表达式

    正则表达式 描述       
    转义字符,将特殊字符进行转义,忽略其特殊意义
    ^ 匹配行首,awk中,^是匹配字符串的开始
    $ 匹配行尾,awk中,$是匹配字符串的结尾
    ^$ 表示空行
    . 匹配除换行符 之外的任意一个字符
    [ ] 匹配包含在[字符]之中的任意单个字符
    [^ ] 匹配[^字符]之外的任意一个字符
    [ - ] 匹配 [ ] 指定范围内的任意一个字符
    ? 匹配之前的项目1次或者0次
    + 匹配之前的项1次或多次
    * 匹配之前的项0次或多次,.*
    ( ) 匹配表达式,创建一个用于匹配的子串
    { n } 匹配之前的项至少需要匹配n次
    {n,} 之前的项至少需要匹配n次
    {n,m} 指定之前的项至少匹配n次,最多匹配m次,n<=m
    |

    交替匹配 | 两边的任意一项ab(c|d)匹配abc 或 abd

    特定字符:

    >[[:space:]]   空格

    [[:digit:]]  [0-9]

    [[:lower:]]  [a-z]

    [[:upper:]]  [A-Z]

    [[:alpha:]]  [a-Z]

    正则表达式练习

    [root@web02 3]# cat test.txt (练习文本)
    I am timfly student!
    I learn linux is very happy.
    test
    
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://timfly.blog.51cto.com
    our site is http://www.timfly123.com
    my qq num is 5223345666666666666.
    not 5728918888873333333.

    命令:

    #过滤率以m开头,以m结尾的行
    [root@web02 3]# grep "^m" test.txt 
    my blog is http://timfly.blog.51cto.com
    my qq num is 5223345666666666666.
    [root@web02 3]# grep "m$" test.txt 
    my blog is http://timfly.blog.51cto.com
    our site is http://www.timfly123.com
    
    
     #排除空行,并打印行号
    [root@web02 3]# grep -vn "^$" test.txt 
    1:I am timfly student!
    2:I learn linux is very happy.
    3:test
    5:I like badminton ball ,billiard ball and chinese chess!
    6:my blog is http://timfly.blog.51cto.com
    7:our site is http://www.timfly123.com
    8:my qq num is 5223345666666666666.
    9:not 5728918888873333333.
    
    
    #匹配任意一个字符,但不包括空行
    [root@web02 3]# grep "." test.txt 
    I am timfly student!
    I learn linux is very happy.
    test
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://timfly.blog.51cto.com
    our site is http://www.timfly123.com
    my qq num is 5223345666666666666.
    not 5728918888873333333
    
    
    #匹配所有
    [root@web02 3]# grep "." test.txt 
    I am timfly student!
    I learn linux is very happy.
    test
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://timfly.blog.51cto.com
    our site is http://www.timfly123.com
    my qq num is 5223345666666666666.
    not 5728918888873333333
    
    
    #匹配单个任意字符
    [root@web02 3]# grep "tim.ly" test.txt 
    I am timfly student!
    my blog is http://timfly.blog.51cto.com
    our site is http://www.timfly123.com
    
    
    #匹配以.结尾的
    [root@web02 3]# grep ".$" test.txt 
    I learn linux is very happy.
    my qq num is 5223345666666666666.
    not 5728918888873333333.
    
    
    #精确匹配到
    [root@web02 3]# grep -o "8*" test.txt 
    8
    88888
    [root@web02 3]# grep -o "tim*" test.txt 
    tim
    tim
    tim
    
    
    #匹配有abc的行,数字所在的行,所有小写字母
    [root@web02 3]# grep "[abc]" test.txt 
    I am timfly student!
    I learn linux is very happy.
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://timfly.blog.51cto.com
    our site is http://www.timfly123.com
    
    [root@web02 3]# grep "[0-9]" test.txt 
    my blog is http://timfly.blog.51cto.com
    our site is http://www.timfly123.com
    my qq num is 5223345666666666666.
    not 5728918888873333333.
    
    [root@web02 3]# grep "[a-z]" test.txt 
    I am timfly student!
    I learn linux is very happy.
    test
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://timfly.blog.51cto.com
    our site is http://www.timfly123.com
    my qq num is 5223345666666666666.
    not 5728918888873333333.
    
    [root@web02 3]# grep "[a-z]" test.txt 
    I am timfly student!
    I learn linux is very happy.
    test
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://timfly.blog.51cto.com
    our site is http://www.timfly123.com
    my qq num is 5223345666666666666.
    not 5728918888873333333.
    
    
    #重复8三次
    [root@web02 3]# grep "8{3}" test.txt    ##带转义字符
    not 5728918888873333333.
    [root@web02 3]# grep "8{3}" test.txt     ##不带转义字符也没有正则
    [root@web02 3]# egrep "8{3}" test.txt     ##正则匹配
    not 5728918888873333333.
    [root@web02 3]# grep -E "8{3}" test.txt     ##加-E 参数
    not 5728918888873333333.
    
    
    #重复数字8,3-5次
    [root@web02 3]# egrep  "8{3,5}" test.txt 
    not 5728918888873333333.
    
    #重复8一次或以上
    [root@web02 3]# grep -E "8{1,}" test.txt 
    not 5728918888873333333.
  • 相关阅读:
    转载——rdis安装yum版本
    Lc28_strStr kmp字符串匹配
    关于 哈希的总结
    Lc344_反转字符串
    Lc383_赎金信
    Lc454_四数相加 II
    Lc1_俩数之和
    推荐4款个人珍藏的IDEA插件!帮你写出不那么差的代码
    ZUC-生成随机序列
    移位运算
  • 原文地址:https://www.cnblogs.com/tim1blog/p/9711429.html
Copyright © 2020-2023  润新知