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 表示引用整个表达式中第二个分组中的正则匹配到的结果



待续。。。。





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯飙的蜗牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值