基础正则表达式(Basic Regular Expression)
序号 |
符号 |
描述 |
示例 |
1 |
^ |
匹配以……开头的行 |
^ab匹配以ab开头的行 |
2 |
$ |
匹配以……结尾的行 |
ab$匹配以ab结尾的行 |
3 |
^$ |
匹配空行 |
^$匹配空行,不匹配空格 |
4 |
. |
匹配任意单个字符 |
ab.匹配abc或abd,不匹配abcd或abde,包括空格 |
5 |
\ |
转义符,将特殊符号进行转义 |
a\.b匹配a.b,不匹配ajb |
6 |
* |
匹配前面项0次或多次 |
ab*匹配a或ab或abbb |
7 |
.* |
匹配任意字符 |
ab.*匹配ab或abc或abcd,包括空行 |
8 |
[] |
匹配集合以内的任意单个字符 |
ab[cd]匹配abc或abd,不匹配abj或abcd |
9 |
[^] |
匹配集合以外的任意单个字符 |
ab[^cd]匹配abe或abj,不匹配abc或abd |
[root@linux-node1 ~]# vim linbin.txt
I am oldboy teacher!
I teach 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 49000488.
not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y
[root@linux-node1 ~]# grep "^m" linbin.txt
my blog is http://oldboy.blog.51cto.com
my qq num is 49000488.
my god,i am not oldbey,but OLDBOY
[root@linux-node1 ~]# grep "m$" linbin.txt
my blog is http://oldboy.blog.51cto.com
[root@linux-node1 ~]# grep -vn "^$" linbin.txt
1:I am oldboy teacher!
2:I teach linux.
4:I like badminton ball,billard ball and chinese chess.
6:my blog is http://oldboy.blog.51cto.com
7:our site is http://www.etiantian.org
9:my qq num is 49000488.
11:not 4900000448.
12:my god,i am not oldbey,but OLDBOY
13:oldb y
[root@linux-node1 ~]# grep "." linbin.txt ##不匹配空行
I am oldboy teacher!
I teach 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 49000488.
not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y
[root@linux-node1 ~]# grep "oldb.y" linbin.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
my god,i am not oldbey,but OLDBOY
oldb y
[root@linux-node1 ~]# grep "\.$" linbin.txt
I teach linux.
I like badminton ball,billard ball and chinese chess.
my qq num is 49000488.
not 4900000448.
[root@linux-node1 ~]# grep "0*" linbin.txt
I am oldboy teacher!
I teach 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 49000488.
not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y
[root@linux-node1 ~]# grep -o "0*" linbin.txt ##精确匹配字符串
000
00000
[root@linux-node1 ~]# grep ".*" linbin.txt ##匹配空行
I am oldboy teacher!
I teach 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 49000488.
not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y
[root@linux-node1 ~]# grep "[abc]" linbin.txt
I am oldboy teacher!
I teach 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 god,i am not oldbey,but OLDBOY
oldb y
[root@linux-node1 ~]# grep "[0-9]" lb.txt
my blog is http://oldboy.blog.51cto.com
my qq num is 49000488.
not 4900000448.ss
[root@linux-node1 ~]# grep "[^a-z]" linbin.txt ##匹配非小写字母
I am oldboy teacher!
I teach 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 49000488.
not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y
[root@linux-node1 ~]# grep "[^A-Z]" linbin.txt ##匹配非大写字母
I am oldboy teacher!
I teach 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 49000488.
not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y
[root@linux-node1 ~]# grep "[^0-9]" linbin.txt ##匹配非数字
I am oldboy teacher!
I teach 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 49000488.
not 4900000448.
my god,i am not oldbey,but OLDBOY
oldb y
扩展正则表达式(Extended Regular Expression)
序号 |
符号 |
描述 |
示例 |
1 |
+ |
匹配前面项1次或多次 |
ab+匹配ab或abb,不匹配a |
2 |
? |
匹配前面项0次或1次 |
ab?匹配a或ab,不匹配abb |
3 |
匹配 |
两边的任意一项 |
ab |
cd匹配ab或cd |
4 |
() |
匹配表达式 |
a(c |
d)e匹配ace或ade,不匹配ae |
5 |
{n,m} |
匹配前面项n~m次 |
ab{2,3}匹配abb或abbb,需要对{} 进行转义 |
6 |
{n,} |
匹配前面项至少n次,包含n次 |
ab{2,}匹配abb或abbb,需要对{} 进行转义 |
7 |
{n} |
匹配前面项n次 |
ab{2}匹配abb,需要对{} 进行转义 |
8 |
{,m} |
匹配前面项最多m次,包含m次 |
ab{,2}匹配a或ab或abb,需要对{} 进行转义 |
[root@linux-node1 ~]# vim linbin.txt
I am oldboy teacher!
I teach 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 49000488.
not 4900000448.
my god,i am not oldbey,but OLDBOY
good
goood
gd
[root@linux-node1 ~]# grep -E "go+d" linbin.txt
my god,i am not oldbey,but OLDBOY
good
goood
[root@linux-node1 ~]# grep -E "go?d" linbin.txt
my god,i am not oldbey,but OLDBOY
gd
[root@linux-node1 ~]# grep -E "god|good" linbin.txt
my god,i am not oldbey,but OLDBOY
good
[root@linux-node1 ~]# grep -E "g(la|oo)d" linbin.txt
good
[root@linux-node1 ~]# egrep "g(la|oo)d" linbin.txt
good
[root@linux-node1 ~]# egrep "g(oo)?d" linbin.txt
good
gd
[root@linux-node1 ~]# grep -E "0{3,5}" linbin.txt
my qq num is 49000488.
not 4900000448.
[root@linux-node1 ~]# grep -E "0{3,}" linbin.txt
my qq num is 49000488.
not 4900000448.
[root@linux-node1 ~]# grep -E "0{3}" linbin.txt
my qq num is 49000488.
not 4900000448.