shell 正则

正则表达式又称为规则表达式

1、位置匹配相关正则

^:    表示锚定行首,此字符后面的任意内容必须出现在行首,才能匹配
$:    表示锚定行尾,此字符后面的任意内容必须出现在行尾,才能匹配
^$:   表示匹配空行,这里的空行表示回车,而空格或tab等不能算作空行
^abc$:表示abc独占一行时,会被匹配到
\<或\b:匹配单词边界,表示锚定词首,其后面的字符必须作为单词首部出现
\>或\b:匹配单词边界,表示锚定词尾,其后面的字符必须作为单词尾部出现
\B:    匹配非单词边界,与\b正好相反

测试文件 test

[root@master ~]# cat test
hello this is test file line one
hello this is test file line two
line three hello
line hello four


hello this is test file line six
 
	
hello
line tenhello
注:第五行为回车;第七行为空格回车;第八行为tab回车;
[root@master ~]# cat -n test
     1	hello this is test file line one
     2	hello this is test file line two
     3	line three hello
     4	line hello four
     5	
     6	hello this is test file line six
     7	 
     8		
     9	hello
    10	line tenhello
    11  line helloeleven
[root@master ~]# grep -n "^hello" test
1:hello this is test file line one
2:hello this is test file line two
6:hello this is test file line six
9:hello
[root@master ~]# grep -n "hello$" test
3:line three hello
9:hello
10:line tenhello
[root@master ~]# grep -n "^$" test
5:
[root@master ~]#  grep -n "^hello$" test
9:hello
[root@master ~]# grep -n "\<hello" test
1:hello this is test file line one
2:hello this is test file line two
3:line three hello
4:line hello four
6:hello this is test file line six
9:hello
11:line helloeleven
[root@master ~]# grep -n "\bhello" test
1:hello this is test file line one
2:hello this is test file line two
3:line three hello
4:line hello four
6:hello this is test file line six
9:hello
11:line helloeleven
[root@master ~]# grep -n "hello\>" test
1:hello this is test file line one
2:hello this is test file line two
3:line three hello
4:line hello four
6:hello this is test file line six
9:hello
10:line tenhello
[root@master ~]#  grep -n "hello\b" test
1:hello this is test file line one
2:hello this is test file line two
3:line three hello
4:line hello four
6:hello this is test file line six
9:hello
10:line tenhello
[root@master ~]# grep -n "\Bhello" test
10:line tenhello
[root@master ~]# grep -n "hello\B" test
11:line helloeleven
[root@master ~]# 

2、连续次数匹配正则

* 表示前面的字符连续出现任意次,包括0次
. 表示跟随任意一个单个字符
.* 表示匹配任意长度的任意字符与*相同
\? 表示匹配其前面的字符0或1次
\+ 表示匹配其前面的字符至少1次
\{x\} 表示之前的字符连续出现x次
\{x,y\} 表示之前的字符至少联系出现x次,至多连续出现y次
\{x,\} 表示之前的字符连续出现至少x次

测试文件test

a a
aa
a aa
bb
bbb
c cc ccc
dddd d dd ddd
ab abc abcc
ef eef eeef
[root@master ~]# cat -n test
     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
[root@master ~]# grep -n "a*" test
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
[root@master ~]# grep -n "a." test
1:a a
2:aa
3:a aa
8:ab abc abcc
[root@master ~]# grep -n "a.*" test
1:a a
2:aa
3:a aa
8:ab abc abcc
[root@master ~]# grep -n "a\?" test
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
[root@master ~]# grep -n "a\+" test
1:a a
2:aa
3:a aa
8:ab abc abcc
[root@master ~]# grep -n "b\{2\}" test
4:bb
5:bbb
[root@master ~]# grep -n "\<b\{2\}\>" test
4:bb
[root@master ~]# grep -n "d\{2,4\}" test
7:dddd d dd ddd
[root@master ~]# grep -n "d\{2,\}" test
7:dddd d dd ddd

3、常用符号

[  ] 表示匹配指定范围内的任意单个字符
[^  ] 表示匹配指定范围外的任意单个字符

[[:alpha:]]  表示任意大小写字母
[[:lower:]]  表示任意小写字母
[[:upper:]]  表示任意大写字母
[[:digit:]]  表示0到9之间的任意单个数字(包括0和9)
[[:alnum:]]  表示任意数字或字母
[[:space:]]  表示任意空白字符,包括"空格"、"tab键"等。
[[:punct:]]  表示任意标点符号
 
[0-9]与[[:digit:]]等效
[a-z]与[[:lower:]]等效
[A-Z]与[[:upper:]]等效
[a-zA-Z]与[[:alpha:]]等效
[a-zA-Z0-9]与[[:alnum:]]等效
 
[^0-9]与[^[:digit:]]等效
[^a-z]与[^[:lower:]]等效
[^A-Z]与[^[:upper:]]等效
[^a-zA-Z]与[^[:alpha:]]等效
[^a-zA-Z0-9]与[^[:alnum:]]等效
 
#简短格式并非所有正则表达式解析器都可以识别
\d 表示任意单个0到9的数字
\D 表示任意单个非数字字符
\t 表示匹配单个横向制表符(相当于一个tab键)
\s表示匹配单q空白字符,包括"空格","tab制表符"等
\S表示匹配单个非空白字符

4、分组

\(\) 表示分组,我们可以将其中的内容当做一个整体,分组可以嵌套
\(ab\) 表示将ab当做一个整体去处理
\1 表示引用整个表达式中第一个分组中的正则匹配到的结果
\2 表示引用整个表达式中第二个分组中的正则匹配到的结果



待续。。。。





猜你喜欢

转载自blog.csdn.net/zhangxueleishamo/article/details/80262500