[root@text ~]# cat d
I love wendy
wendy,you are beautiful
I can do it
welcome to beijing
I want to shenzhen
[root@text ~]# grep "w*" d
I love wendy
wendy,you are beautiful
I can do it
welcome to beijing
I want to shenzhen
字符
描述
.*
任意多个字符
[root@text ~]# egrep ".*" d
I love wendy
wendy,you are beautiful
I can do it
welcome to beijing
I want to shenzhen
字符
描述
^.*
以任意多个字符串开头,.*尽可能多,有多少算多少,贪婪性
[root@text ~]# grep "^.*w" d
I love wendy
wendy,you are beautiful
welcome to beijing
I want to shenzhen
#这里是匹配以w开头的行
[root@text ~]# grep "^[w]" d
[root@text ~]# grep "^[w]" d
wendy,you are beautiful
welcome to beijing
#这里匹配的事不包含^面字符
[root@text ~]# grep "[^w]" d
I love wendy
wendy,you are beautiful
I can do it
welcome to beijing
I want to shenzhen
括号表达式
描述
a\{n,m\}
重复前面a字符n到m次(如果用egrep或sed -r可去掉斜线)
[root@text ~]# vim e
[root@text ~]# cat e
a{1,3}
I love wendy
wendy,you are beautiful
I can do it
welcome to beijing
I want to shenzhen
[root@text ~]# grep "a{1,3}" e
a{1,3}
[root@text ~]# egrep "a{1,3}" e
a{1,3}
wendy,you are beautiful
I can do it
I want to shenzhen
括号表达式
描述
a{n,\}
重复前面a字符至少n次,如果用egrep或sed -r可去掉斜线
a{n\}
重复前面a字符n次,如果用egrep或sed -r可去掉斜线
第8章 扩展正则表达式ERE
特殊字符
含义与例子
+
重复前一个字符一次或一次以上,前一个字符连续一个或多个,把连续的文本/字符取出
[root@text ~]# echo "www.foa.com" >>d
[root@text ~]# cat d
I love wendy
wendy,you are beautiful
I can do it
welcome to beijing
I want to shenzhen
www.foa.com
[root@text ~]# grep "^[wd]+" d
#因为grep不支持正则,所以要加转义字符\
[root@text ~]# grep "^[wd]\+" d
wendy,you are beautiful
welcome to beijing
www.foa.com
[root@text ~]# egrep "^[wd]+" d
wendy,you are beautiful
welcome to beijing
www.foa.com