Shell脚本下的BRE正则表达式指令

举报
tea_year 发表于 2025/11/28 17:17:33 2025/11/28
【摘要】 Linux 系统中文本处理工具(如 grep、sed、awk)依赖正则表达式实现高效匹配,核心分为基本正则表达式(BRE)与扩展正则表达式(ERE)两种风格,二者在元字符使用与工具适配性上差异显著。正则表达式在 Linux 系统中广泛应用于文本处理工具如 grep,sed,awk 等。Linux 主要支持两种正则表达式风格:基本正则表达式(BRE)和 扩展正则表达式(ERE)。基本正则表达式...
Linux 系统中文本处理工具(如 grep、sed、awk)依赖正则表达式实现高效匹配,核心分为基本正则表达式(BRE)与扩展正则表达式(ERE)两种风格,二者在元字符使用与工具适配性上差异显著。正则表达式在 Linux 系统中广泛应用于文本处理工具如 grep,sed,awk 等。Linux 主要支持两种正则表达式风格:基本正则表达式(BRE)扩展正则表达式(ERE)
  1. 基本正则表达式(BRE)

BRE 是 Linux 中许多工具默认使用的正则表达式语法(如 grep 不加 -E 选项时)
[root@localhost day07]# grep  'root\|adm' /etc/passwd使用了扩展元字符|   需要\转义
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost day07]# grep -E  'root|adm' /etc/passwd指定-E 使用扩展元字符
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost day07]# egrep    'root|adm' /etc/passwd使用egrep 使用了扩展元字符
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
  1. 基本元字符

  • . 匹配任意单个字符(除换行符) \. 代表.
[root@localhost day07]# grep  '^a\.' 1.txt
a.x
  • ^ 匹配行首
[root@localhost day07]# grep '^root' passwd
root:x:0:0:root:/root:/bin/bash
  • $ 匹配行尾
[root@localhost day07]# grep 'nologin$' passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
  • * 前导字符出现零次或多次
[root@localhost day07]# grep 'roo*' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
setroubleshoot:x:989:989:SELinux troubleshoot server:/var/lib/setroubleshoot:/usr/sbin/nologin
chrony:x:987:986:chrony system user:/var/lib/chrony:/sbin/nologin
[] 字符集合
grep '[a-d]' /etc/passwd
  • [^] 否定字符集合 除了.....
grep '[^a]' /etc/passwd

2.特殊转义序列

  • \d 数字字符(某些工具不支持,建议用[0-9])
[root@localhost day07]# grep -P '\d' /etc/passwd 
  • \w 单词字符(字母、数字、下划线),等同于[a-zA-z0-9_]
  • \s 空白字符(空格、制表符等) 等同于[\t]
  • \b 单词边界
[root@localhost day07]# grep 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
rootxy:x:1027:1027::/home/rootxy:/bin/bash
xyzroot:x:1028:1028::/home/xyzroot:/bin/bash
[root@localhost day07]# grep '\broot' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
rootxy:x:1027:1027::/home/rootxy:/bin/bash
[root@localhost day07]# grep 'root\b' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
xyzroot:x:1028:1028::/home/xyzroot:/bin/bash
[root@localhost day07]# 
  • \< 和 \> 单词开始和结束(Linux 特有)
[root@localhost day07]# grep 'root\b' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
xyzroot:x:1028:1028::/home/xyzroot:/bin/bash
[root@localhost day07]# grep 'root\>' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
xyzroot:x:1028:1028::/home/xyzroot:/bin/bash

3.扩展正则表达式(ERE)

使用 grep -E 或 egrep 时启用 ERE 语法,支持更多元字符而不需要转义。
  1. 扩展元字符

  • + 前导字符出现一次或多次
[root@localhost day07]# grep -E 'ro+' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
setroubleshoot:x:989:989:SELinux troubleshoot server:/var/lib/setroubleshoot:/usr/sbin/nologin
chrony:x:987:986:chrony system user:/var/lib/chrony:/sbin/nologin
roox:x:1024:1024::/home/roox:/bin/bash
rootxy:x:1027:1027::/home/rootxy:/bin/bash
xyzroot:x:1028:1028::/home/xyzroot:/bin/bash
xrooooot:x:1029:1029::/home/xrooooot:/bin/bash
  • ? 前导字符出现零次或一次
[root@localhost day07]# grep -E 'ro?' /etc/passwd
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
  • | 或操作
[root@localhost day07]# grep -E 'root|adm' /etc/passwd
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
rootxy:x:1027:1027::/home/rootxy:/bin/bash
xyzroot:x:1028:1028::/home/xyzroot:/bin/bash
  • ()-分组
[root@localhost day07]# grep -E '^abc+' /etc/passwd
abcbc:x:1025:1025::/home/abcbc:/bin/bash
abcde:x:1026:1026::/home/abcde:/bin/bash
abccccc:x:1030:1030::/home/abccccc:/bin/bash

[root@localhost day07]# grep -E '^a(bc){2}' /etc/passwd
abcbc:x:1025:1025::/home/abcbc:/bin/bash

[root@localhost day07]# grep -E '^abc{2}' /etc/passwd
abccccc:x:1030:1030::/home/abccccc:/bin/bash
  • {} 量词(不需要转义)
  • {m} 匹配m次
[root@localhost day07]# grep 'ro\{2\}' /etc/passwd # 除了基本元字符都需要\转义
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
roox:x:1024:1024::/home/roox:/bin/bash
rootxy:x:1027:1027::/home/rootxy:/bin/bash
xyzroot:x:1028:1028::/home/xyzroot:/bin/bash

[root@localhost day07]# grep -E 'ro{2}' /etc/passwd # 直接使用-E 不用转义
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
roox:x:1024:1024::/home/roox:/bin/bash
rootxy:x:1027:1027::/home/rootxy:/bin/bash
xyzroot:x:1028:1028::/home/xyzroot:/bin/bash
  • {n,} 至少匹配n次
[root@localhost day07]# grep -E 'ro{1,}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
setroubleshoot:x:989:989:SELinux troubleshoot server:/var/lib/setroubleshoot:/usr/sbin/nologin
chrony:x:987:986:chrony system user:/var/lib/chrony:/sbin/nologin
roox:x:1024:1024::/home/roox:/bin/bash
rootxy:x:1027:1027::/home/rootxy:/bin/bash
xyzroot:x:1028:1028::/home/xyzroot:/bin/bash
xrooooot:x:1029:1029::/home/xrooooot:/bin/bash
  • {n,m} 匹配n到m次
[root@localhost day07]# grep -E 'ro{1,3}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
setroubleshoot:x:989:989:SELinux troubleshoot server:/var/lib/setroubleshoot:/usr/sbin/nologin
chrony:x:987:986:chrony system user:/var/lib/chrony:/sbin/nologin
roox:x:1024:1024::/home/roox:/bin/bash
rootxy:x:1027:1027::/home/rootxy:/bin/bash
xyzroot:x:1028:1028::/home/xyzroot:/bin/bash
xrooooot:x:1029:1029::/home/xrooooot:/bin/bash

总结

BRE 是 grep 等工具默认模式,元字符需通过反斜杠转义才能生效。例如匹配 “root” 或 “adm” 需grep 'root\|adm' /etc/passwd,量词{m}需写为\{m\};其基础元字符涵盖.(匹配任意单字符)、^(行首)、$(行尾)、*(前导字符零次或多次)及[](字符集合),可满足基础匹配需求,grep '^root' /etc/passwd筛选行首为 root 的记录,grep 'nologin$' /etc/passwd匹配行尾为 nologin 的行。
ERE 需通grep -Eegrep启用,支持更多元字符且无需转义,功能更强大。例grep -E 'root|adm' /etc/passwd直接实现 “或” 匹配,+(前导字符一次或多次)、?(零次或一次)、()(分组)及{n,m}(量词)可精准控制匹配规则,grep -E '^a(bc){2}' /etc/passwd匹配行首为 “abcbc” 的内容,grep -E 'ro{1,3}' /etc/passwd匹配含 1-3 个 “o” 的 “ro” 组合。
此外,特殊转义序列(如\d匹配数字、\s匹配空白字符、\b匹配单词边界)需结grep -P启用 Perl 兼容模式,而 Linux 特有\<\>可替代\b标识单词开始与结束。实际应用中,需根据匹配复杂度选择 BRE(基础场景)或 ERE(复杂规则),并注意工具与正则风格的适配,避免因转义问题导致匹配失效。

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。