您的位置:首页 > 运维架构 > Shell

shell脚本(14)-正则表达式

2021-08-07 13:19 1016 查看

一、正则表达式介绍

正则表达式是一种文本模式匹配,包括普通字符(a...z)和特殊字符(元字符)。

它是一种字符串匹配模式,可以用来检查一个字符串是否含有某种子串、将匹配的子串替换或者从某个字符串中取出某个条件的子串

shell支持正则表达式,但是不是所有的命令都支持正则,常见的命令中只有grep、sed、awk命令支持正则表达式

 

二、特殊字符

1、定位符使用-模糊匹配与精准匹配:

同时锚定开头和结尾,做精确匹配;单一锚定开发和结尾,做模糊匹配。

定位符 说明
^ 锚定开头^a以a开发,默认锚定一个字符
$ 锚定结尾a$以a结尾,默认锚定一个字符

 举例说明:定位符

[root@localhost test20210731]# egrep "^abbbc$" file  #正则匹配,等价于grep -e 或 grep -E,精确匹配
abbbc
[root@localhost test20210731]# egrep "^ab" file #匹配开头为ab
abbbc
abababa
abC
[root@localhost test20210731]# egrep "bb$" file #匹配结尾为bb
aabb
&abbb
bbbb

2、匹配符-匹配字符串:

匹配符 说明
. 匹配除回车以外的任意字符
() 字符串分组
[] 定义字符串,匹配括号中的一个字符 
[^] 表示否定括号中出现字符串的字符,取反
\ 转义字符
| 管道-或,结合分组使用

 举例说明匹配符:

[root@localhost test20210806]# egrep "^a.c$" file  #匹配a开头,c结尾,中间任意字符
aBc
aYc
a*c
a4c
a9c
a7c
[root@localhost test20210801]# egrep "^a[0-9]c$" file #匹配a开头c结尾,中间的字符为0-9
a4c
a9c
a7c
[root@localhost test20210801]# egrep "^a[^0-9]c$" file #匹配a开头c结尾,中间非数字
aBc
aYc
a*c
[root@localhost test20210801]# egrep "^a\*c$" file #精确匹配a*c的情况
a*c
[root@localhost test20210801]# egrep "^a*c$" file #不加转义无法匹配
ac
[root@localhost test20210801]# egrep "^(a|b)c$" file #精确匹配以a或b开头,c结尾
ac
bc

3、限定符-对前面的符合或字符串做限定说明

限定符 说明
* 某个字符之后加星号表示该字符不出现或出现多次
与型号类似,但略有不行,表示该字符出现一次或不出现
+ 与星号类似,表示其前面字符出现一次或多次,但是至少出现一次
{n,m} 某个字符之后出现,表示该字符最少n次,最多m次
{m} 某个字符出现m次

 举例说明限定符:

[root@localhost test20210806]# egrep "^ab*c$" file  #ab字符中匹配有b(全部需要是b)或没有b
abbbc
ac
[root@localhost test20210806]# egrep "^ab*c$" file  #ab字符中匹配有b(全部需要是b)或没有b
abbbc
ac
abc
[root@localhost test20210806]# egrep "^ab?c$" file  #ab字符中匹配有b(出现一次)或没有b
ac
abc
[root@localhost test20210806]# egrep "^ab+c$" file  #ac字符中匹配有b(至少出现一次)
abbbc
abc
[root@localhost test20210806]# egrep "^ab*c$" file  #ac字符中匹配有b(全部需要是b)或没有b
abbbc
ac
abc
[root@localhost test20210806]# egrep "^ab?c$" file  #ac字符中匹配有b(出现一次)或没有b
ac
abc
[root@localhost test20210806]# egrep "^ab+c$" file  #ac字符中匹配有b(至少出现一次)
abbbc
abc
[root@localhost test20210806]# egrep "^ab{1,3}c$" file  #ac字符中匹配有b(出现在1次到3次内)
abbbc
abc
[root@localhost test20210806]# egrep "^ab{3}c$" file  #ac字符中匹配有b(正好出现3次)
abbbc

 

三、POSIX字符

特殊字符 说明
[:alnum:] 匹配任意字母字符0-9 a-z A-Z
[:alpha:] 匹配任意字母,大写或小写
[:dight:] 数字0-9
[:graph:] 非空字符(非空格控制字符)
[:lower:] 小写字符a-z
[:upper:] 大写字符A-Z
[:cntrl:] 控制字符
[:print:] 非空字符(包括空格)
[:punct:] 标点符号
[:blank:] 空格和TAB字符
[:xdigit:] 16进制数字
[:space:] 所有空白字符(新行、空格、制表符)

 注意:[[]]双中括号的意思:第一个中括号是匹配符[]匹配中括号中的任意一个字符,第二个[]格式如[:digit:]

举例说明:

[root@localhost tesr20210807]# egrep "^a[[:alnum:]]c$" file  #a开头c结尾,中间一个字符匹配非特殊符号
aBc
aYc
a4c
a9c
a7c
abc
[root@localhost tesr20210807]# egrep "^a[[:alnum:]]c$" file  #a开头c结尾,中间一个字符匹配任意字母
aBc
aYc
a4c
a9c
a7c
abc
[root@localhost tesr20210807]# egrep "^a[[:alnum:]]c$" file  #a开头c结尾,中间一个字符匹配非特殊符号
aBc
aYc
a4c
a9c
a7c
abc
[root@localhost tesr20210807]# egrep ^C]c$" file  #a开头c结尾,中间一个字符匹配任意字母
[root@localhost tesr20210807]# egrep "^a[[:alnum:]]c$" file  #a开头c结尾,中间一个字符匹配非特殊符号
aBc
aYc
a4c
a9c
a7c
abc
[root@localhost tesr20210807]# egrep "^a[[:alpha:]]c$" file  #a开头c结尾,中间一个字符匹配任意字母
aBc
aYc
abc
[root@localhost tesr20210807]# egrep "^a[[:digit:]]c$" file  #a开头c结尾,中间一个字符匹配任意数字
a4c
a9c
a7c
[root@localhost tesr20210807]# egrep "^a[[:graph:]]c$" file  #a开头c结尾,中间一个字符匹配非空字符
aBc
aYc
a*c
a4c
a9c
a7c
abc
a,c
[root@localhost tesr20210807]# egrep "^a[[:lower:]]c$" file  #a开头c结尾,中间一个字符匹配小写字母
abc
[root@localhost tesr20210807]# egrep "^a[[:upper:]]c$" file  #a开头c结尾,中间一个字符匹配大写字母
aBc
aYc
[root@localhost test20210807]# egrep "^a[[:blank:]]c$" file #a开头c结尾,中间一个字符为空格或TAB
a c
a c
[root@localhost test20210807]# egrep "^a[[:space:]]c$" file #a开头c结尾,中间匹配所有空白、空行、制表符
a c
a c
[root@localhost test20210807]# egrep "^a[[:blank:]]c$" file #a开头c结尾,中间一个字符为空格或TAB
a c
a c
[root@localhost test20210807]# egrep "^a[[:space:]]c$" file #a开头c结尾,中间匹配所有空白、空行、制表符
a c
a c
[root@localhost test20210807]# egrep "^a[[:blank:]]c$" file #a开头c结尾,中间一个字符为空格或TAB
a c
a c
[root@localhost test20210807]# egrep "^a[[:space:]]c$" file #a开头c结尾,中间一个字符匹配空白、空行、制表符
a c
a c
[root@localhost test20210807]# egrep "^a[[:punct:]]c$" file #a开头c结尾,中间一个字符匹配标点符号
a*c
a,c
[root@localhost test20210807]# egrep "^a[[:print:]]c$" file #a开头c结尾,中间一个字符匹配非空字符(含括号)
aBc
aYc
a*c
a4c
a9c
a7c
abc
a c
a,c
[root@localhost test20210807]# egrep "^a[[:xdigit:]]c$" file #a开头c结尾,中间一个字符匹配十六进制数
aBc
a4c
a9c
a7c
abc

 

 四、常见正则匹配:

1、数字:^[0-9]*$

2、数字或英文字母:^[A-Za-z0-9]+$

2、英文字母:^[A-Za-z]+$

3、日期格式(2021-05-01或2021-5-1):[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} 

4、手机号码:^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$

5、IP地址:^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\.

6、E-mail:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$

7、身份证(15位或18位):(^[1-9][0-9]{5}(18|19|([23][0-9]))[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{3}[0-9Xx]$)|(^[1-9][0-9]{5}[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{2}$)

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: