Linux正则表达式

正则表达式是一种字符模式,用于在查找过程中匹配制定的字符。
元字符通常在Linux中分为两类:
Shell元字符,由Linux Shell进行解析;
正则表达式元字符,由vi/grep/sed/awk等文本处理工具进行解析;
正则表达式一般以文本行进行处理,在进行下面实例之前,先为grep命令设置--color参数:

[root@node01 mnt]# alias grep='grep --color=auto'

这样每次过滤出来的字符串都会带色彩了。
在开始之前还需要做一件事情,就是创建如下两个文件:

[root@node01 mnt]# cat re-file 
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love
is forever. I live for you. It's hard to get back in the
groove.
[root@node01 mnt]# cat linux.txt 
Linux is a good 
god assdxw bcvnbvbjk
greatttttt  wexcvxc
operaaaating  dhfghfvx
gooodfs awrerdxxhkl
gdsystem awxxxx
glad
good

正则表达式元字符

元字符 功能
^ 行以什么开头
$ 行以什么结尾
. 匹配任意单个字符
* 匹配任意0次或多次字符串
[xy] 匹配x或y中的任意单个字符
[x-y] 匹配x~y范围内的任意单个字符
[^x-y] 匹配不在x~y范围内的任意单个字符
\ 转义字符

特殊的元字符

元字符 功能
< 单词以什么开头
> 单词以什么开头
() \1 标签匹配以后使用的字符串,最大支持9个
{n} 匹配任意n次字符串
{n,} 匹配任意n次或多次字符串
{n,m} 匹配任意n次到m次字符串

扩展的正则表达式

元字符 功能
+ 匹配任意1次或多次字符串
? 匹配任意0次或1次字符串
| 表示或
() 分组过滤匹配

实例

  1. 匹配以love开头的所有行
[root@node01 mnt]# grep '^love' re-file 
love, how much I adore you. Do you know
  1. 匹配以love结尾的所有行
[root@node01 mnt]# grep 'love$' re-file 
clover. Did you see them?  I can only hope love
  1. 匹配包含以l开头、中间包含两个字符、结尾是e的字符串的所有行
[root@node01 mnt]# grep 'l..e' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love
is forever. I live for you. It's hard to get back in the
  1. 匹配包含0个或多个空行、后面是love的字符串的所有行
[root@node01 mnt]# grep ' *love' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love
  1. 匹配包含love或Love的所有行
[root@node01 mnt]# grep '[lL]ove' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love
  1. 匹配包含以A-Z开头、后面是ove的字符串的所有行
[root@node01 mnt]# grep '[A-Z]ove' re-file
Lovers were all around us. It is springtime. Oh
  1. 匹配包含小写字符的所有行
[root@node01 mnt]# grep '[^A-Z]' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love
is forever. I live for you. It's hard to get back in the
groove.
  1. 匹配包含love的所有行
[root@node01 mnt]# grep 'love' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love
  1. 匹配包含空格的所有行
[root@node01 mnt]# grep ' ' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love
is forever. I live for you. It's hard to get back in the
  1. 匹配包含任意字符的所有行
[root@node01 mnt]# grep '.*' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love
is forever. I live for you. It's hard to get back in the
groove.
  1. 匹配包含o重复2~4次的字符串的所有行
[root@node01 mnt]# grep -E 'o{2,4}' re-file
groove.
  1. 匹配包含重复o最少2次的字符串的所有行
[root@node01 mnt]# grep -E 'o{2,}' re-file
groove.
  1. 匹配包含重复o最多2次的字符串的所有行
[root@node01 mnt]# grep -E 'o{0,2}' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them?  I can only hope love
is forever. I live for you. It's hard to get back in the
groove.
  1. 匹配包含以g开头、重复o最少1次、以d结尾的字符串的所有行
[root@node01 mnt]# grep -E 'go+d' linux.txt 
Linux is a good 
god assdxw bcvnbvbjk
gooodfs awrerdxxhkl
good
  1. 匹配包含以g开头、重复o最多1次、以d结尾的字符串的所有行
[root@node01 mnt]# grep -E 'go?d' linux.txt 
god assdxw bcvnbvbjk
gdsystem awxxxx
  1. 匹配包含gd或good的所有行
[root@node01 mnt]# grep -E 'gd|good' linux.txt 
Linux is a good 
gdsystem awxxxx
good
  1. 匹配包含glad或good的所有行
[root@node01 mnt]# grep -E 'g(la|oo)d' linux.txt 
Linux is a good 
glad
good
  1. 匹配包含以g开头和结尾的字符串的所有行
[root@node01 mnt]# grep -E '(g).*\1' linux.txt 
operaaaating  dhfghfvx

常用正则表达式公式总结

一、校验数字的表达式

  1. 数字:^[0-9]*$
  2. n位的数字:^d{n}$
  3. 至少n位的数字:^d{n,}$
  4. m~n位的数字:^d{m,n}$
  5. 零和非零开头的数字:^(0|[1-9][0-9]*)$
  6. 非零开头的最多带两位小数的数字:^([1-9][0-9]*)+(.[0-9]{1,2})?$
  7. 带1~2位小数的正数或负数:^(-)?d+(.d{1,2})$
  8. 正数、负数、和小数:^(-|+)?d+(.d+)?$
  9. 有两位小数的正实数:^[0-9]+(.[0-9]{2})?$
  10. 有1~3位小数的正实数:^[0-9]+(.[0-9]{1,3})?$
  11. 非零的正整数:^[1-9]d*$^([1-9][0-9]*){1,3}$^+?[1-9][0-9]*$
  12. 非零的负整数:^-[1-9][]0-9"*$^-[1-9]d*$
  13. 非负整数:^d+$^[1-9]d*|0$
  14. 非正整数:^-[1-9]d*|0$^((-d+)|(0+))$
  15. 非负浮点数:^d+(.d+)?$^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$
  16. 非正浮点数:^((-d+(.d+)?)|(0+(.0+)?))$^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$
  17. 正浮点数:^[1-9]d*.d*|0.d*[1-9]d*$^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$
  18. 负浮点数:^-([1-9]d*.d*|0.d*[1-9]d*)$^(-(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*)))$
  19. 浮点数:^(-?d+)(.d+)?$^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$

二、校验字符的表达式

  1. 汉字:^[一-龥]{0,}$
  2. 英文和数字:^[A-Za-z0-9]+$^[A-Za-z0-9]{4,40}$
  3. 长度为3~20的所有字符:^.{3,20}$
  4. 由26个英文字母组成的字符串:^[A-Za-z]+$
  5. 由26个大写英文字母组成的字符串:^[A-Z]+$
  6. 由26个小写英文字母组成的字符串:^[a-z]+$
  7. 由数字和26个英文字母组成的字符串:^[A-Za-z0-9]+$
  8. 由数字、26个英文字母或者下划线组成的字符串:^w+$^w{3,20}$
  9. 中文、英文、数字包括下划线:^[一-龥A-Za-z0-9_]+$
  10. 中文、英文、数字但不包括下划线等符号:^[一-龥A-Za-z0-9]+$^[一-龥A-Za-z0-9]{2,20}$
  11. 禁止输入含有~的字符:[^~"]+

三、特殊需求表达式

  1. Email地址:^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$
  2. 域名:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?
  3. InternetURL:[a-zA-z]+://[^s]*^http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$
  4. 手机号码:^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])d{8}$
  5. 国内电话号码(0511-4405222、021-87888822):d{3}-d{8}|d{4}-d{7}
  6. 电话号码(支持手机号码,34位区号,78位直播号码,1~4位分机号):((d{11})|^((d{7,8})|(d{4}|d{3})-(d{7,8})|(d{4}|d{3})-(d{7,8})-(d{4}|d{3}|d{2}|d{1})|(d{7,8})-(d{4}|d{3}|d{2}|d{1}))$)
  7. 身份证号(15位、18位数字),最后一位是校验位,可能为数字或字符X:(^d{15}$)|(^d{18}$)|(^d{17}(d|X|x)$)
  8. 帐号是否合法(字母开头,允许5~16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
  9. 密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线):^[a-zA-Z]w{5,17}$
  10. 强密码(必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8~10之间):^(?=.*d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,10}$
  11. 强密码(必须包含大小写字母和数字的组合,可以使用特殊字符,长度在8~10之间):^(?=.*d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
  12. 日期格式:^d{4}-d{1,2}-d{1,2}$
  13. 一年的12个月(0109和112):^(0?[1-9]|1[0-2])$
  14. 一个月的31天(0109和131):^((0?[1-9])|((1|2)[0-9])|30|31)$
  15. XML文件:^([a-zA-Z]+-?)+[a-zA-Z0-9]+\.[x|X][m|M][l|L]$
  16. 中文字符:^[一-龥]$
  17. 空白行:s*
  18. HTML标记:<(S*?)[^>]*>.*?|<.*? />
  19. 腾讯QQ号:[1-9][0-9]{4,}
  20. 中国邮政编码:[1-9]d{5}(?!d)
  21. IP地址:((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))

正则表达式在线测试

你可能感兴趣的:(Linux正则表达式)