10.2 正则表达式示例
在前面的第5章中,我们了解了grep的一些基本用法,但是它的功能还远远不止这些。grep的英文是Global search Regular Expression and print out the line,即全面搜索正则表达式并打印出匹配行。通过本章前面的一些实例我们也看到,grep和正则表达式结合使用后产生了强大的搜索效果。本节将通过更多的示例来介绍正则表达式和grep结合的用法,帮助大家更深入地理解和认识正则表达式和grep。提醒一下读者,由于正则表达式中含有较多特殊的字符,所以结合grep时,最好使用单引号将正则表达式括起来,以免造成错误。
为了演示grep命令的用法,首先创建一个文件RegExp.txt,文件内容如下所示:
- [root@localhost ~]# cat RegExp.txt
----TEXT BEGIN----
good morning teacher
hello world is a script
gold sunshine looks beautiful
golden time flies
god bless me
what a delicious food
they teast Good
you fell glad
wrong word gooood
wrong word gl0d
wrong word gl2d
wrong word gl3d
www.helloworld.com
www@helloworld@com
Upper case
100% means pure
php have a gd module
-----TEXT END-----
接下来,回顾一下grep的基本用法:
#
搜索含有good
单词的行
#
注意:Grep
默认是区分大小写的,所以这里只会打印出包含小写good
的行
[root@localhost ~]# grep 'good' RegExp.txt
good morning teacher
#
搜索含有good
单词的行,不区分大小写
[root@localhost ~]# grep -i 'good' RegExp.txt
good morning teacher
they teast Good
#
统计不含good
单词的行的行数,不区分大小写
[root@localhost ~]# grep -ivc 'good' RegExp.txt
19
下面可以正式介绍正则表达式和grep结合的用法了。
1)使用“^”匹配行首,示例如下:
#
搜索以good
开头的行
[root@localhost ~]# grep '^good' RegExp.txt
good morning teacher
2)使用“$”匹配行尾,示例如下:
#
搜索以Good
结尾的行
[root@localhost ~]# grep 'Good$' RegExp.txt
they teast Good
3)使用“^$”组合,匹配空行,下面的命令可计算文件中共有多少行空行:
#
搜索空行的行数
[root@localhost ~]# grep -c '^$' RegExp.txt
2
4)使用方括号匹配多种可能,示例如下:
#
搜索包含Good
和good
的行
[root@localhost ~]# grep '[Gg]ood' RegExp.txt
good morning teacher
they teast Good
5)在方括号中使用“^”做反选,示例如下:
#
搜索一个包含ood
的行,但是不能是Good
或good
#
记住在方括号中使用尖角号表示的是“非”
[root@localhost ~]# grep '[^Gg]ood' RegExp.txt
what a delicious food
wrong word gooood
6)使用“.”号,示例如下:
#
搜索包含一个词,该词以g
开头、紧接着是两个任意字符、再接着是一个d
的行
[root@localhost ~]# grep 'g..d' RegExp.txt
good morning teacher
gold sunshine looks beautiful
golden time flies
you fell glad
wrong word gl0d
wrong word gl2d
wrong word gl3d
#
搜索包含一个词,该词以G
或g
开头、紧接着是两个任意字符、再接着是一个d
的行
[root@localhost ~]# grep '[Gg]..d' RegExp.txt
good morning teacher
gold sunshine looks beautiful
golden time flies
they teast Good
you fell glad
wrong word gl0d
wrong word gl2d
wrong word gl3d
#
搜索这样一些行,该行包含某个单词,该词满足如下条件:
1.
第一个字符可以是G
或g
2.
第二个字符可以是l
或o
3.
第三个字符可以是换行符之外的任意字符
4.
第四个字符一定是d
[root@localhost ~]# grep '[Gg][lo].d' RegExp.txt
good morning teacher
gold sunshine looks beautiful
golden time flies
they teast Good
you fell glad
wrong word gl0d
wrong word gl2d
wrong word gl3d
7)使用精确匹配,示例如下:
#
搜索含有gold
的行
#
从搜索结果中发现golden
也被匹配出来了
[root@localhost ~]# grep 'gold' RegExp.txt
gold sunshine looks beautiful
golden time flies
#
正如上例所示,一般搜索时,想要搜索含有gold
的行,发现golden
也匹配了
#
现在我们需要精确匹配含有gold
这个单词的行
[root@localhost ~]# grep '\<gold\>' RegExp.txt
gold sunshine looks beautiful
#
用“\b
”的效果和“\<\>
”一致
[root@localhost ~]# grep '\bgold\b' RegExp.txt
gold sunshine looks beautiful
8)使用“*”号,示例如下:
#
搜索这样一些行,该行包含某个单词,该词满足如下条件:
1.
以g
开头
2.g
后面跟零到无限个o
3.
零到无限个o
后面跟d
[root@localhost ~]# grep go*d RegExp.txt
good morning teacher
god bless me
wrong word gooood
php have a gd module
9)使用“.*”号,示例如下:
#
搜索这样一些行,该行包含某个单词,该词满足如下条件:
1.
以g
开头
2.g
后面一定有字符
3.
最后是d
[root@localhost ~]# grep 'g.*d' RegExp.txt
good morning teacher
gold sunshine looks beautiful
golden time flies
god bless me
you fell glad
wrong word gooood
wrong word gl0d
wrong word gl2d
wrong word gl3d
php have a gd module
10)使用“-”号,示例如下:
#
文件中有一些拼写错误的单词,发现是把glod
中的o
字母写成数字0
了
[root@localhost ~]# grep 'gl[0-9]d' RegExp.txt
wrong word gl0d
wrong word gl2d
wrong word gl3d
11)使用“\”做字符转义,示例如下:
#
搜索文件中包含域名www.helloworld.com
的行
#
从搜索的结果来看,这里的“.
”号被解析成了除换行符外的任意字符
#
想要把这个点只当作一个字符点来用,就需要对其使用转义符
[root@localhost ~]# grep 'www.helloworld.com' RegExp.txt
www.helloworld.com
www@helloworld@com
#
这里将点做转义,则输出的结果满足预期
[root@localhost ~]# grep 'www.helloworld.com' RegExp.txt
www.helloworld.com
12)使用“{}”号,示例如下:
#
文档中有一个单词good
被拼写错了,多写了几个o
#
搜索以字母g
开头包含两个以上o
的单词
[root@localhost ~]# grep 'go{2,}' RegExp.txt
good morning teacher
wrong word gooood
#
搜索以字母g
开头,中间正好包含4
个o
的单词
[root@localhost ~]# grep 'go{4}' RegExp.txt
wrong word gooood
13)特殊的POSIX字符,示例如下:
grep支持一类特殊的POSIX字符,列举如下
- #[:alnum:]
文字数字字符
#[:alpha:]
文字字符
#[:digit:]
数字字符
#[:graph:]
非空字符(
非空格、控制字符)
#[:lower:]
小写字符
#[:cntrl:]
控制字符
#[:print:]
非空字符(
包括空格)
#[:punct:]
标点符号
#[:space:]
所有空白字符(
新行,空格,制表符)
#[:upper:]
大写字符
#[:xdigit:]
十六进制数字(0-9
,a-f
,A-F)
#
搜索以大写字母开头的行
[root@localhost ~]# grep ^[[:upper:]] RegExp.txt
Upper case
#
搜索以数字开头的行
[root@localhost ~]# grep ^[[:digit:]] RegExp.txt
100% means pure
14)使用扩展的正则表达式egrep,示例如下:
#
搜索g
和d
之间至少有一个o
的行
#
“+
”代表匹配前面的字符1
次以上(含1
次)
[root@localhost ~]# egrep 'go+d' RegExp.txt
good morning teacher
god bless me
wrong word gooood
#
搜索g
和d
之间只有0
个或1
个o
的行(0
次或1
次)
#
“?
”代表匹配前面的字符1
次以上
[root@localhost ~]# egrep 'go?d' RegExp.txt
god bless me
php have a gd module
#
搜索有glad
或gold
的行
[root@localhost ~]# egrep 'glad|gold' RegExp.txt
gold sunshine looks beautiful
golden time flies
you fell glad
#
搜索有glad
或gold
的行的另一种写法
[root@localhost ~]# egrep 'g(la|ol)d' RegExp.txt
gold sunshine looks beautiful
golden time flies
you fell glad
从以上的例子可以看出,正则表达式为文件行搜索提供了强大的支持,使得搜索更为灵活,但同时也加大了使用和读写难度。要解决这个问题,只有不断地多读多用,才能较为深刻地理解正则表达式。