标签:ESS ace 字符 roo dmi admin 搜索 grep -E 扩展正则表达式
一、基础正则表达式
-i 忽略大小写
-n 显示匹配的行号
-c 统计匹配的行数,不显示搜索结果
-o 仅显示匹配到的字符
-q 不显示结果
(1)^ :匹配以什么开头 --- ^ab表示匹配以ab开头的行;
(2)$:匹配以什么结尾的行 --- ab$表示匹配以 ab结尾的行;
(3)^$:匹配空行 --- 表示匹配空行,不匹配空格;
(4). :匹配任意单个字符 --- ab.表示表示匹配abc或abc,不匹配abcd或abce,包括空格
(5)\ :转义符,将特殊符号进行转义 ------ a\.b表示匹配a.b的行,转义点的实际属性,而不代表任意字符;
(6)* :匹配前面项0次或者多次 --- ab* 表示匹配a或者ab或者b或者abb,只要匹配ab字符其中一次或者多次的行;
(7).* :匹配任意字符 --- ab.* 表示匹配ab或者或者abc或者abd,包括空格;
(8)[] :匹配集合以内的任意单个字符 --- ab[cd] 表示匹配abc或者abc,不匹配abe或者abf;
(9)[^] :匹配集合以外的任意单个字符 ---ab[^cd] b表示匹配abe或abf,不匹配abc,abd;
1、表达式测试数据
[root@centos6 /]# cat test
I am oldboy teacher !
Iteach linux.
I like badminton ball,billard ball and chinese chess.
my blog is http//:oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000048
not 4900000048
my god,i am not oldbey,but OLDBOY
oldb y
(1)^ :匹配以什么开头
[root@centos6 /]# grep "^m" test
my blog is http//:oldboy.blog.51cto.com
my qq num is 49000048
my god,i am not oldbey,but OLDBOY
(2)$:匹配以什么结尾的行
[root@centos6 /]# grep "m$" test
my blog is http//:oldboy.blog.51cto.com
(3)^$:匹配空行(用-vn参数排查空行,只显示非空行)
[root@centos6 /]# grep -vn "^$" test
1:I am oldboy teacher !
2:Iteach linux.
5:I like badminton ball,billard ball and chinese chess.
7:my blog is http//:oldboy.blog.51cto.com
8:our site is http://www.etiantian.org
12:my qq num is 49000048
15:not 4900000048
16:my god,i am not oldbey,but OLDBOY
18:oldb y
(4). :匹配任意单个字符
[root@centos6 /]# grep "m." test
I am oldboy teacher !
I like badminton ball,billard ball and chinese chess.
my blog is http//:oldboy.blog.51cto.com
my qq num is 49000048
my god,i am not oldbey,but OLDBOY
(5)\ :转义符,将特殊符号进行转义
[root@centos6 /]# grep "\.$" test #以点结尾的行
Iteach linux.
I like badminton ball,billard ball and chinese chess.
(6)* :匹配前面项0次或者多次
[root@centos6 /]# grep -o "0*" test #精确匹配显示匹配的数据
0000
000000
(7).* :匹配任意字符
[root@centos6 /]# grep ".*" test 全部匹配,包括空格
I am oldboy teacher !
Iteach linux.
I like badminton ball,billard ball and chinese chess.
my blog is http//:oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000048
not 4900000048
my god,i am not oldbey,but OLDBOY
oldb y
(8)[] :匹配集合以内的任意单个字符
[root@centos6 /]# grep "[0-9]" test #匹配有数字的行
my blog is http//:oldboy.blog.51cto.com
my qq num is 49000048
not 4900000048
(9)[^] :匹配集合以外的任意单个字符
[root@centos6 /]# grep "[^0-9]" test #匹配非数字的行;
I am oldboy teacher !
Iteach linux.
I like badminton ball,billard ball and chinese chess.
my blog is http//:oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000048
not 4900000048
my god,i am not oldbey,but OLDBOY
oldb y
二、扩展正则表达式
测试数据
[root@centos6 ~]# cat test1
I am oldboy teacher !
Iteach linux.
I like badminton ball,billard ball and chinese chess.
my blog is http//:oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000048
not 4900000048
my god,i am not oldbey,but OLDBOY
good
goood
gd
(1)+ :匹配前面1次或多次 ---ab+ 表示匹配ab或abb,不匹配a;
[root@centos6 ~]# grep -E "go+d" test1
my god,i am not oldbey,but OLDBOY
good
goood
(2)?:匹配前面项0次或者1次 ---ab?表示匹配a或者ab,不匹配abb;
[root@centos6 ~]# grep -E "go?d" test1
my god,i am not oldbey,but OLDBOY
gd
(3)| :匹配两边的任意一项 ---ab|cd 表示匹配ab或者cd;
[root@centos6 ~]# grep -E "god|good" test1
my god,i am not oldbey,but OLDBOY
good
(4)() :匹配表达式 ---a(cd)e 表示匹配ace或ade,不匹配ae;
[root@centos6 ~]# grep -E "g(a|oo)d" test1
good
[root@centos6 ~]# grep -E "g(oo)?d" test1
good
gd
(5){n,m} :匹配前面项n~m次 ---ab{2,3}表示匹配abb或abbb,需要对{}进行转义;
[root@centos6 ~]# grep -E "0{3,5}" test1
my qq num is 49000048
not 4900000048
(6){n,} :匹配前面项至少n次,包含n次 ---ab{2,} 表示匹配abb或abbb,需要对{}进行转义;
[root@centos6 ~]# grep -E "0{3,}" test1
my qq num is 49000048
not 4900000048
(7){n} :匹配前面项n次 ---ab{2}匹配abb,需要对{}进行转义;
[root@centos6 ~]# grep -E "O{1}" test1
my god,i am not oldbey,but OLDBOY
(8){,m}: 匹配前面项最多m次,包含m次, ---ab{,2}匹配a或者ab或者abb,需要对{}转义;
标签:ESS ace 字符 roo dmi admin 搜索 grep -E 扩展正则表达式
原文地址:https://www.cnblogs.com/jiangzuofenghua/p/9278787.html