2)匹配次数
用法:用在指定重复出现字符的后面
功能:限制前面的支付要出现的次数
*:表示匹配其前面的字符任意次(0或1或多次)【Jasonforcto注:这里要区分开通配符的定义】
案例: a,b,ab,aab,acb,adb,amnb
因此对于以上的案例中"a*b"只能匹配(b,ab,aab,)
详解:对于a*b表示的是a是一个需要重复的字符,而*表示重复的次数,b表示的是a后面的字符
因此:当*等于0时,匹配的是b
当*等于1时,匹配的是ab
当*等于2时,匹配的是aab
这里一定要注意*表示的是任意次,而不是任意字符
[root@linux ~]# grep '.*' test.txt
This is a beautiful girl
So do you want to know who is her?
oh! I can`t tell you?
can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you ?
在看看下面一个,要注意这两个的结合
[root@linux ~]# grep '.' test.txt
This is a beautiful girl
So do you want to know who is her?
oh! I can`t tell you?
can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you ?
或者看看下面的理解了吗?
[root@linux ~]# grep -v '.' test.txt
[root@linux ~]#
.*:表示的是任意长度的任意字符(此处的*是重复.的次数)
[root@linux ~]# grep '.*' test.txt
This is a beautiful girl
So do you want to know who is her?
oh! I can`t tell you?
can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you ?
案例:t.*e表示的是t开头,e结尾"."可以是任意字符"*"表示.可以出现多次
[root@linux ~]# grep 't.*e' test.txt
So do you want to know who is her?
oh! I can`t tell you?
can you tell me your phone number?
My telphone number is 15648562351...
这个命令把所有符合的条件都显示了出来,为什么是一行呢?这里大家注意一下,Linux系统中正则表达式默认的处于贪婪模式,就是尽可能的把所有匹配到的行都显示出来。可是如何关闭贪婪模式呢?下面一个我们就讲到了j_0057.gif
注意: 默认工作处于贪婪模式
贪婪 与 非贪婪(惰性) 两者的前提都是需要匹配成功,区别在于:
贪婪 是在匹配成功的前提下,尽可能多的去匹配
非贪婪(惰性)是在匹配成功的前提下,尽可能少的去匹配
?:表示匹配其前面的字符可有可无(1次或0次)
[root@linux ~]# grep 'a\?b' test002.txt
b
ab
acb
aab
ahjhb
ababababab
[root@linux ~]#
#注意在grep中需要给特殊字符转义比如"{},?,<>"以下就不多说明了\{m,n\}:匹配其前面的字符至少m次,至多n次
[root@linux ~]# grep 'a\{1,3\}b' test002.txt
ab
aab
ababababab
[root@linux ~]#\{1,\}:表示匹配至少一次
[root@linux ~]# grep 'a\{1,\}b' test002.txt
ab
aab
ababababab
[root@linux ~]#\{0,2\}:表示最多匹配2次
[root@linux ~]# grep 'a\{0,2\}b' test002.txt
b
ab
acb
aab
ahjhb
ababababab
[root@linux ~]#3)位置锚定
说明:限制使用模式搜索文本,限制模式所匹配到的文本只能出现于目标文本的个位置;
字符
描述
^
行首锚定;用于模式的最左侧,^PATTERN
$
行尾锚定;用于模式的最右侧,PATTERN$
^PATTERN$
要让PATTERN完全匹配一整行
^$
没有任何字符包括空格的行
^[[:space:]]*$
没有任何字符但是包括空格的行
^:
[root@linux ~]# grep '^r..t' /etc/passwd
root:x:0:0:root:/root:/bin/bash
##如果不加上首字符,其他的行内包含的r..t也会显示出来的
[root@linux ~]# grep 'r..t' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin$:
[root@linux ~]# grep 't$' /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
[root@linux ~]#^$:
[root@linux ~]# grep -n '^$' test002.txt
8:
[root@linux ~]#
单词:由非特殊字符组成的连续字符(字符串)都称为单词;
\
###第一个例子
[root@linux ~]# grep "\
this is super
this is a super
this is a big super
[root@linux ~]#
##########################################
[root@linux ~]# grep "super\>" test003.txt
this is super
this is a super
this is a big super
super is me
he super is hei
[root@linux ~]####第二个例子
[root@linux ~]# grep "\" /etc/passwd
games:x:12:100:games:/usr/games:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin
polkitd:x:997:995:User for polkitd:/:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
colord:x:996:994:User for colord:/var/lib/colord:/sbin/nologin
libstoragemgmt:x:995:992:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
setroubleshoot:x:994:991::/var/lib/setroubleshoot:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
chrony:x:993:990::/var/lib/chrony:/sbin/nologin
geoclue:x:992:989:User for geoclue:/var/lib/geoclue:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
Jason:x:1000:1000::/home/Jason:/bin/bash
[root@linux ~]#
\>或\b:词尾锚定,用于单词模式的右侧,格式为PATTERN\>, PATTERN\b
[root@linux ~]# fdisk -l | grep "/dev/[sh]d[a-z]\>"
Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 sectors
[root@linux ~]#[root@linux ~]# grep "super\>" test003.txt
this is super
this is a super
this is a big super
super is me
he super is hei
[root@linux ~]#\:单词锚定;
[root@linux ~]# grep "\(is\)*" test003.txt
this is super
this is a super
this is a big super
super is me
he super is hei
[root@linux ~]#2)扩展正则表达式
1) 字符匹配的命令和用法与基本正则表达式的用法相同,这里不再重复阐述。
2) 次数匹配:
* :匹配其前面字符的任意次
[root@linux ~]# cat muli.txt
good god goood food foolish gooooooooooogle
[root@linux ~]# egrep 'o*' muli.txt
good god goood food foolish gooooooooooogle
[root@linux ~]#.* :表示匹配任意字符
[root@linux ~]# cat muli.txt
good god goood food foolish gooooooooooogle
[root@linux ~]# egrep 'l.*' muli.txt
good god goood food foolish gooooooooooogle
[root@linux ~]#?:匹配其前面字符的0次或着1次
[root@linux ~]# egrep 'hi?' test003.txt
this is super
this is a super
this is a big super
he super is hei+ :匹配其前面字符至少1次
[root@linux ~]# egrep 'hi+' test003.txt
this is super
this is a super
this is a big super{m,n} :匹配其前面字符m到n次
[root@linux ~]# egrep 'hi{1,2}' test003.txt
this is super
this is a super
this is a big super 3) 位置锚定的用法和基本正则表达式的用法相同,在此不再阐述。
分组及引用:
(pattern):分组,括号中的模式匹配到的字符会被记录于正则表达式引擎内部的变量中;
后向引用:\1, \2, ...
[root@linux ~]# grep 'l..e.*l..e' test004.txt
he like his liker
he love his lover
he like his likes
he love his lovers[root@linux ~]# grep '\(l..e\).*\1' test004.txt
he like his liker
he love his lover
he like his likes
he love his lovers或者:
a|b:a或者b
[root@linux ~]# egrep 'C|is' test3.txt
Cat is not cat!
[root@linux ~]# C|cat:表示C或cat
[root@linux ~]# cat test3.txt
Cat is not cat!
[root@linux ~]# egrep 'C|cat' test3.txt
Cat is not cat!
[root@linux ~]#(C|c)at:表示Cat或cat
[root@linux ~]# cat test3.txt
Cat is not cat!
[root@linux ~]# egrep '(C|c)at' test3.txt
Cat is not cat!
[root@linux ~]#