测试文件regex.txt如下
[root@192 Zhengze]# cat -n regex.txt a a aa a aa bb bbb c cc ccc dddd d dd ddd ab abc abcc ef eef eeef
搜索2个a
[root@192 Zhengze]# grep --color -n "aa" regex.txt
2:aa
3:a aa
等同于如下
[root@192 Zhengze]# grep --color -n "a{2}" regex.txt
2:aa
3:a aa
但是多于2个也会被匹配到
[root@192 Zhengze]# grep --color -n "b{2}" regex.txt
4:bb
5:bbb
通过锚定精确匹配到2个b
[root@192 Zhengze]# grep --color -n "<b{2}>" regex.txt
4:bb
z{x,y} 表示z字符至少连续出现x次,最多连续出现y次
[root@192 Zhengze]# grep --color -n "d{2,4}" regex.txt
7:dddd d dd ddd
那么{x,}表示之前的字符至少连续出现x次,或者连续出现次数大于x次,即可被匹配到
{,y}表示之前的字符至多连续出现y次,或者连续出现次数小于y次,即可被匹配到,最小次数为0次,换句话说,之前的字符连续出现0次到y次,都会被匹配到。
[root@192 Zhengze]# grep --color -n "d{2,}" regex.txt
7:dddd d dd ddd
ab都小于2,cc小于等于2,所以被匹配到了
[root@192 Zhengze]# grep --color -n "abc{,2}" regex.txt
8:ab abc abcc
*表示之前的字符连续出现任意次数(包括0次)
[root@192 Zhengze]# grep --color -n "e*f" regex.txt
9:ef eef eeef
d连续出现了0次,其他也符合条件所以被输出了
[root@192 Zhengze]# grep --color -n "d*" regex.txt
1:a a
2:aa
3:a aa
4:bb
5:bbb
6:c cc ccc
7:dddd d dd ddd
8:ab abc abcc
9:ef eef eeef
10:
通配符中,*表示匹配任意长度的任意字符
在正则表达式中,使用".*"表示任意长度的任意字符