加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式

发布时间:2020-12-13 19:54:26 所属栏目:百科 来源:网络整理
导读:正则表达式 工作中公司需要对外招聘人才,但大千世界人才众多并不一定每个都适合你,这时我们可以有很多方法找到你需要的人。常用的方式有两种,第一,通过朋友介绍直接精准的定位到人;第二,写招聘简章(对需要的人才仅需描述:学历、经验、技能、语言等等

正则表达式

工作中公司需要对外招聘人才,但大千世界人才众多并不一定每个都适合你,这时我们可以有很多方法找到你需要的人。常用的方式有两种,第一,通过朋友介绍直接精准的定位到人;第二,写招聘简章(对需要的人才仅需描述:学历、经验、技能、语言等等),写完后通过招聘会、网络招聘等方式广纳人才 ,通常描述写的越细越能快速精准的定位所需人才。
而正则表达式是一种计算机描述语言,你可以直接告诉计算机你需要的是字母A来精确匹配定位,也可以告诉计算机你需要的是26个字母中的任意一个匹配,等等。现在很多程序、文本编辑工具、编程语言都支持正则表达式,但任何语言都需要遵循一定的语法规则,正则表达式也不例外,正则表达式的发展经历了基本正则表达式与扩展正则表达式,扩展正则表达式是在基本正则表达式的基础上添加了一些更加丰富的匹配规则而成。在Linux世界中有句古老的语言“Everything is a file(一切皆文件)”,而且很多配置文件是纯文本文件,工作中我们时常需要对大量的服务器进行配置的修改,如果以手动的方式在海量数据中进行查找匹配并最终完成修改,此时,使用正则表达式是非常明智的选择。接下来,我们分别看看每种表达式的具体规则。由于不同的工具对正则的支持有所不同,表3-3列举了系统常用编辑工具与正则表达式的对应关系。
注意正则表达式有些匹配字符与Shell中的通配符符号一样,但含义却不同。注意正则表达式有些匹配字符与Shell中的通配符符号一样,但含义却不同。
1-1

编辑工具

基本正则表达式

扩展正则表达式

grep

egrep

vi

sed

awk

1.1 基本正则表达式(Regular Expression

1-2列出了基本正则表达式及其对应的含义。
1-2

字符

含义

c

匹配字母c

.

匹配任意单个字符

*

匹配前一个字符出现零次或多次

.*

匹配任意字符

[]

匹配集合中的任意单个字符,括号中为一个集合

[x-y]

匹配连续的字串范围

^

匹配字串的开头

$

匹配字串的结尾

[^]

匹配否定,对括号中的集合取反

匹配转义后的字串

{n,m}

匹配前一个字符重复nm

{n,}

匹配前一个字符重复至少n

{n}

匹配前一个字符重复n

()

()之间的内容存储在“保留空间”,最大存储9

n

通过19调用保留空间中的内容

2. 基本正则表达式案例

提示:由于模版文件的内容在每个系统略有差异,以下案例的输出结果可能有所不同。
 
 
  1. [root@centos6~]#cp/etc/passwd /tmp/#复制模版文件
  2. 查找包含root的行:
  3. [root@centos6~]#greproot/tmp/passwd
  4. root:x:0:0:root:/root:/bin/bash
  5. operator:x:11:0:operator:/root:/sbin/nologin

查找:0:之间包含任意两个字符的字串,并显示该行(--color代表以颜色加亮显示匹配的内容):

 
 
  1. [root@centos6~]#grep--color:..0:/tmp/passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. sync:x:5:0:sync:/sbin:/bin/sync
  4. shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
  5. halt:x:7:0:halt:/sbin:/sbin/halt
  6. games:x:12:100:games:/usr/games:/sbin/nologin
  7. avahi-autoipd:x:170:170:AvahiIPv4LLStack:/var/lib/avahi-autoipd:/sbin/nologin

查找包含至少一个0的行(第一个0必须出现,第二个0可以出现0次或多次):

 
 
  1. (由于输出内容较多案例中仅为部分输出)
  2. [root@centos6~]#grep--color00*/tmp/passwd
  3. root:x:0:0:root:/root:/bin/bash#该行有两处匹配
  4. sync:x:5:0:sync:/sbin:/bin/sync
  5. shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
  6. halt:x:7:0:halt:/sbin:/sbin/halt
  7. uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
  8. operator:x:11:0:operator:/root:/sbin/nologin
  9. games:x:12:100:games:/usr/games:/sbin/nologin#匹配0出现2次
  10. gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
  11. ftp:x:14:50:FTPUser:/var/ftp:/sbin/nologin
  12. avahi-autoipd:x:170:170:AvahiIPv4LLStack:/var/lib/avahi-autoipd:/sbin/nologin
  13. avahi:x:70:70:AvahimDNS/DNS-SDStack:/var/run/avahi-daemon:/sbin/nologin
  14. 查找包含oot或ost的行:
  15. [root@centos6~]#grep--coloro[os]t/tmp/passwd
  16. root:x:0:0:root:/root:/bin/bash
  17. operator:x:11:0:operator:/root:/sbin/nologin
  18. postfix:x:89:89::/var/spool/postfix:/sbin/nologin
  19. 查找包含0-9数字的行:
  20. [root@centos6~]#grep–color[0-9]/tmp/passwd
  21. root:x:0:root:/root:/bin/bash
  22. bin:x:1:1:bin:/bin:/sbin/nologin
  23. daemon:x:2:2:daemon:/sbin:/sbin/nologin
  24. 查找包含f-q字母的行:
  25. [root@centos6~]#grep--color[f-q]/tmp/passwd
  26. root:x:0:0:root:/root:/bin/bash
  27. bin:x:1:1:bin:/bin:/sbin/nologin
  28. 查找以root开头的行:
  29. [root@centos6~]#grep--color^root/tmp/passwd
  30. root:x:0:0:root:/root:/bin/bash
  31. 查找以bash结尾的行:
  32. [root@centos6~]#grep--colorbash$/tmp/passwd
  33. root:x:0:0:root:/root:/bin/bash
  34. 查找sbin/后面不跟n的行:
  35. [root@centos6~]#grep--colorsbin/[^n]/tmp/passwd
  36. shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
  37. halt:x:7:0:halt:/sbin:/sbin/halt
  38. 查找数字0出现1次2次的行:
  39. [root@centos6~]#grep--color'0{1,2}'/tmp/passwd
  40. root:x:0:root:/root:/bin/bash
  41. sync:x:5:0:sync:/sbin:/bin/sync
  42. 查找包含两个root的行(注意,grep在使用()过滤时,匹配条件必须使用引号):
  43. [root@centos6test]#grep--color"(root).*1"/tmp/passwd
  44. root:x:0:0:root:/root:/bin/bash
  45. 查找包含root:开头:root结尾的字串行:
  46. [root@centos6test]#grep--color"(root)(:).*21"/tmp/passwd
  47. root:x:0:0:root:/root:/bin/bash
  48. 过滤文件的空白行:
  49. [root@centos6test]#grep^$/tmp/passwd
  50. 过滤文件的非空白行:
  51. [root@centos6test]#grep-v^$/tmp/passwd

1.2 扩展正则表达式(Extended Regular Expression
1.
1-3列出了扩展正则表达式及其对应的含义。
1-3

字符

含义

{n,m}

等同于基本正则表达式的{n,m}

+

匹配前一个字符出现一次或多次

?

匹配前一个字符出现零次或一次

|

匹配逻辑或者,即匹配|前或后的字串

()

匹配正则集合

2.扩展正则表达式案例
由于输出信息与基本正则表达式类似,这里仅写出命令而不再打印输出信息。
查找数字0出现12次的行:

 
 
  1. [root@centos6~]#egrep--color'0{1,2}'/tmp/passwd
  2. 查找包含至少一个0的行:
  3. [root@centos6~]#egrep--color‘0+’/tmp/passwd
  4. 查找包含root或者admin的行:
  5. [root@centos6~]#egrep--color‘(root|admin)’/tmp/passwd

丁丁历险http://manual.blog.51cto.com/3300438/1135296

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读