扩展正则学习

2020-11-04  本文已影响0人  蓝山_d851

仅供自己学习参考

扩展正则基础正则的加强版。

物料准备

[root@test001 tmp]# wget http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt

删除空行和注释行#

[root@test001 tmp]# sed  's/^#//g' regular_express.txt  |  sed  '/^$/d'   
"Open Source" is a good mechanism to develop programs!
apple is my favorite food!
Football game is not use feet only!
this dress doesn't fit me!

###扩展正则方式
[root@test001 tmp]# egrep  -v '^#|^$'  regular_express.txt 
"Open Source" is a good mechanism to develop programs!
apple is my favorite food!
Football game is not use feet only!
this dress doesn't fit me!
However, this dress is about $ 3183 dollars!
GNU is free air not free beer!
Her hair is very beauty!
I can't finish the test!
Oh! The soup taste good!
更方便

特殊符号意义与范例
”+ “ 重复一个或者一个以上的前一个字符,如搜寻 good god goood 等字符串,那个o+ 代表一个以上的o (至少一个)

[root@test001 tmp]# egrep   -n 'go+g'   regular_express.txt 
18:google is the best tools for search keyword.
19:goooooogle yes!

"?" 零个或者1个前一个字符串 (最多一个)

[root@test001 tmp]# egrep  -n 'go?g'   regular_express.txt 

"|" 用or的方式找出多个条件的字符串

[root@test001 tmp]# egrep  -n  'gd|good|dog'  regular_express.txt 
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
14:The gd software is a library for drafting programs.
17:I like dog.

"()" 找出群组字符串

root@test001 tmp]# egrep  -n  'gd|good|dog'  regular_express.txt 
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
14:The gd software is a library for drafting programs.
17:I like dog.
[root@test001 tmp]# egrep   -n  'g(la|oo)d'  regular_express.txt 
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with "glad".

"()+"多个重复群组的判断

###找出 开头是A结尾是C中间是xyz的多个重复字符串
[root@test001 tmp]# echo  AxyzxyzxyzxyzxyzC   
AxyzxyzxyzxyzxyzC
[root@test001 tmp]# echo  AxyzxyzxyzxyzxyzC     |  egrep   'A(xyz)+C'
AxyzxyzxyzxyzxyzC
上一篇 下一篇

猜你喜欢

热点阅读