shell文本处理之正则表达式

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、正则表达式是什么?
  • 二、正则表达式分类
    • 1.基础正则表达式
      • 查找特定字符
      • 利用中括号“[]”来查找集合字符
      • 查找行首“^”与行尾字符“$”
      • 查找任意一个字符“.”与重复字符“*”
    • 1.扩展正则表达式
  • 总结


前言

正则表达式又称正规表达式、常规表达式。在代码中常简写为 regex、regexp 或 RE。
正则表达式是使用单个字符串来描述、匹配一系列符合某个句法规则的字符串,简单来说,
是一种匹配字符串的方法,通过一些特殊符号,实现快速查找、删除、替换某个特定字符串。


一、正则表达式是什么?

正则表达式是由普通字符与元字符组成的文字模式。模式用于描述在搜索文本时要匹
配的一个或多个字符串。正则表达式作为一个模板,将某个字符模式与所搜索的字符串进
行匹配。其中普通字符包括大小写字母、数字、标点符号及一些其他符号,元字符则是指
那些在正则表达式中具有特殊意义的专用字符,可以用来规定其前导字符(即位于元字符
前面的字符)在目标对象中的出现模式

正则表达式一般用于脚本编程与文本编辑器中。很多文本处理器与程序设计语言均支持
正则表达式,例如 Linux 系统中常见的文本处理器(grep、egrep、sed、awk)以及应用比
较广泛的 Python 语言。正则表达式具备很强大的文本匹配功能,能够在文本海洋中快速高
效地处理文本。

二、正则表达式分类

1.基础正则表达式

正则表达式的字符串表达方法根据不同的严谨程度与功能分为基本正则表达式与扩展
正则表达式。基础正则表达式是常用正则表达式最基础的部分。在 Linux 系统中常见的文件
处理工具中 grep 与 sed 支持基础正则表达式,而 egrep 与 awk 支持扩展正则表达式。掌握
基础正则表达式的使用方法,首先必须了解基本正则表达式所包含元字符的含义,下面通过
grep 命令以举例的方式逐个介绍。

基础正则表达式的元字符主要包括以下几个

  • ^ 匹配输入字符串的开始位置。除非在方括号表达式中使用,表示不包含该字符集合。要匹配“^” 字符本身,请使用“^”
  • $ 匹配输入字符串的结尾位置。如果设置了 RegExp 对象的 Multiline 属性,则“KaTeX parse error: Undefined control sequence: \n at position 6: ”也匹配‘\̲n̲’或‘\r’。 要匹配“”字符本身,请使用“$”
  • . 匹配除“\r\n”之外的任何单个字符
  • \反斜杠,又叫转义字符,去除其后紧跟的元字符或通配符的特殊意义
  • * 匹配前面的子表达式零次或多次。要匹配“*”字符,请使用“*”
  • [] 字符集合。匹配所包含的任意一个字符。例如,“[abc]”可以匹配“plain”中的“a”
  • [^] 赋值字符集合。匹配未包含的一个任意字符。例如,“[^abc]”可以匹配“plain”中任何一个字母
  • [n1-n2] 字符范围。匹配指定范围内的任意一个字符。例如,“[a-z]”可以匹配“a”到“z”范围内的任意一个小写字母字符。 注意:只有连字符(-)在字符组内部,并且出现在两个字符之间时,才能表示字符的范围;如果出现在字符组的开头,则只能表示连字符本身

代码如下:

[root@localhost ~]# cat test.txt
he was short and fat. He was wearing a blue polo shirt with black pants. 
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword. 
The year ahead will test our political establishment to the limit. 
PI=3.141592653589793238462643383249901429

a wood cross!
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.

查找特定字符

查找特定字符非常简单,如执行以下命令即可从 test.txt 文件中查找出特定字符“the”所
在位置。其中“-n”表示显示行号、“-i”表示不区分大小写。命令执行后,符合匹配标准的字符,
字体颜色会变为红色(本章中全部通过加粗显示代替)。

[root@localhost ~]# grep -n 'the' test.txt
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword. 6:The year ahead will test our political establishment to the limit.
[root@localhost ~]# grep -in 'the' test.txt
3:The home of Football on BBC Sport online. 4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword. 6:The year ahead will test our political establishment to the limit.

若反向选择,如查找不包含“the”字符的行,则需要通过 grep 命令的“-v”选项实现,并
配合“-n”一起使用显示行号。

[root@localhost ~]# grep -vn 'the' test.txt
1:he was short and fat. 2:He was wearing a blue polo shirt with black pants. 3:The home of Football on BBC Sport online. 7:PI=3.141592653589793238462643383249901429
8:a wood cross!
9:Actions speak louder than words
10:
11:#woood #
12:#woooooood #
13:AxyzxyzxyzxyzC
14:I bet this place is really spooky late at night!
15:Misfortunes never come alone/single. 16:I shouldn't have lett so tast.

利用中括号“[]”来查找集合字符

想要查找“shirt”与“short”这两个字符串时,可以发现这两个字符串均包含“sh”与“rt”。此
时执行以下命令即可同时查找到“shirt”与“short”这两个字符串,其中“[]”中无论有几个字符,
都仅代表一个字符,也就是说“[io]”表示匹配“i”或者“o”。

[root@localhost ~]# grep -n 'sh[io]rt' test.txt
1:he was short and fat. 2:He was wearing a blue polo shirt with black pants

若要查找包含重复单个字符“oo”时,只需要执行以下命令即可。

[root@localhost ~]# grep -n 'oo' test.txt
3:The home of Football on BBC Sport online. 5:google is the best tools for search keyword. 8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!

若查找“oo”前面不是“w”的字符串,只需要通过集合字符的反向选择“[^]”来实现该目的。
例如执行“grep -n‘[^w]oo’test.txt”命令表示在 test.txt 文本中查找“oo”前面不是“w”的字符串。

[root@localhost ~]# grep -n '[^w]oo' test.txt
3:The home of Football on BBC Sport online. 5:google is the best tools for search keyword. 11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!

在上述命令的执行结果中发现“woood”与“wooooood”也符合匹配规则,二者均包含“w”。
其实通过执行结果就可以看出,符合匹配标准的字符加粗显示,而上述结果中可以得知,
“#woood #”中加粗显示的是“ooo”,而“oo”前面的“o”是符合匹配规则的。同理“#woooooood #” 也符合匹配规则。
若不希望“oo”前面存在小写字母,可以使用“grep -n‘[^a-z]oo’test.txt”命令实现,其中
“a-z”表示小写字母,大写字母则通过“A-Z”表示。

[root@localhost ~]# grep -n '[^a-z]oo' test.txt
3:The home of Football on BBC Sport online.

查找包含数字的行可以通过“grep -n‘[0-9]’test.txt”命令来实现。

[root@localhost ~]# grep -n '[0-9]' test.txt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429

查找行首“^”与行尾字符“$”

基础正则表达式包含两个定位元字符:“^”(行首)与“$”(行尾)。在上面的示例中,
查询“the”字符串时出现了很多包含“the”的行,如果想要查询以“the”字符串为行首的行,则
可以通过“^”元字符来实现。

[root@localhost ~]# grep -n '^the' test.txt
4:the tongue is boneless but it breaks bones.12!

查询以小写字母开头的行可以通过“1”规则来过滤,查询大写字母开头的行则使用
[A-Z]”规则,若查询不以字母开头的行则使用“[^a-zA-Z]”规则。

[root@localhost ~]# grep -n '^[a-z]' test.txt
1:he was short and fat. 4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword. 8:a wood cross!
[root@localhost ~]# grep -n '^[A-Z]' test.txt
2:He was wearing a blue polo shirt with black pants. 3:The home of Football on BBC Sport online. 6:The year ahead will test our political establishment to the limit. 7:PI=3.141592653589793238462643383249901429
9:Actions speak louder than words
13:AxyzxyzxyzxyzC
14:I bet this place is really spooky late at night!
15:Misfortunes never come alone/single. 16:I shouldn't have lett so tast.
[root@localhost ~]# grep -n '^[^a-zA-Z]' test.txt
11:#woood #
12:#woooooood #

“^”符号在元字符集合“[]”符号内外的作用是不一样的,在“[]”符号内表示反向选择,在“[]” 符号外则代表定位行首。反之,若想查找以某一特定字符结尾的行则可以使用“$”定位符。
例如,执行以下命令即可实现查询以小数点(.)结尾的行。因为小数点(.)在正则表达式
中也是一个元字符(后面会讲到),所以在这里需要用转义字符“\”将具有特殊意义的字符转
化成普通字符。

[root@localhost ~]# grep -n '\.$' test.txt
1:he was short and fat. 2:He was wearing a blue polo shirt with black pants. 3:The home of Football on BBC Sport online. 5:google is the best tools for search keyword. 6:The year ahead will test our political establishment to the limit. 15:Misfortunes never come alone/single. 16:I shouldn't have lett so tast.

当查询空白行时,执行“grep -n‘^$’test.txt”命令即可。

[root@localhost ~]# grep -n '^$' test.txt
10:

查找任意一个字符“.”与重复字符“*”

前面提到,在正则表达式中小数点(.)也是一个元字符,代表任意一个字符。例如执
行以下命令就可以查找“w??d”的字符串,即共有四个字符,以 w 开头 d 结尾。

[root@localhost ~]# grep -n 'w..d' test.txt
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words

在上述结果中,“wood”字符串“w…d”匹配规则。若想要查询 oo、ooo、ooooo 等资料,
则需要使用星号()元字符。但需要注意的是,“”代表的是重复零个或多个前面的单字符。
“o*”表示拥有零个(即为空字符)或大于等于一个“o”的字符,因为允许空字符,所以执行“grep
-n ‘o*’ test.txt”命令会将文本中所有的内容都输出打印。如果是“oo*”,则第一个 o 必须存在,
第二个 o 则是零个或多个 o,所以凡是包含 o、oo、ooo、ooo,等的资料都符合标准。同
理,若查询包含至少两个 o 以上的字符串,则执行“grep -n ‘ooo*’ test.txt”命令即可。

[root@localhost ~]# grep -n 'ooo*' test.txt
3:The home of Football on BBC Sport online. 5:google is the best tools for search keyword. 8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!

查询以 w 开头 d 结尾,中间包含至少一个 o 的字符串,执行以下命令即可实现。

[root@localhost ~]# grep -n 'woo*d' test.txt
8:a wood cross!
11:#woood #
12:#woooooood #

执行以下命令即可查询以 w 开头 d 结尾,中间的字符可有可无的字符串。

[root@localhost ~]# grep -n 'w.*d' test.txt
1:he was short and fat. 5:google is the best tools for search keyword. 8:a wood cross!
9:Actions speak louder than words
11:#woood #
12:#woooooood #

执行以下命令即可查询任意数字所在行。

[root@localhost ~]# grep -n '[0-9][0-9]*' test.txt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429

1.扩展正则表达式

通常情况下会使用基础正则表达式就已经足够了,但有时为了简化整个指令,需要使用
范围更广的扩展正则表达式。例如,使用基础正则表达式查询除文件中空白行与行首为“#”
第 11 页 共 27 页
之外的行(通常用于查看生效的配置文件),执行“grep -v‘^KaTeX parse error: Expected group after '^' at position 21: ….txt | grep -v‘^̲#’”即可实现。 这里需要使用…|^#’test.txt”,其中,单引号内的管道符号表示或者(or)。
此外,grep 命令仅支持基础正则表达式,如果使用扩展正则表达式,需要使用 egrep
或 awk 命令。awk 命令在后面的小节进行讲解,这里我们直接使用 egrep 命令。egrep 命
令与 grep 命令的用法基本相似。egrep 命令是一个搜索文件获得模式,使用该命令可以搜
索文件中的任意字符串和符号,也可以搜索一个或多个文件的字符串,一个提示符可以是单
个字符、一个字符串、一个字或一个句子。
与基础正则表达式类型相同,扩展正则表达式也包含多个元字符,常见的扩展正则表达
式的元字符主要包括以下几个

  • + 作用:重复一个或者一个以上的前一个字符
    示例:执行“egrep -n ‘wo+d’ test.txt”命令,即可查询"wood" “woood” "woooooood"等字符串
  • ? 作用:零个或者一个的前一个字符
    示例:执行“egrep -n ‘bes?t’ test.txt”命令,即可查询“bet”“best”这两个字符串
  • | 作用:使用或者(or)的方式找出多个字符
    示例:执行“egrep -n ‘of|is|on’ test.txt”命令即可查询"of"或者"if"或者"on"字符串
  • () 作用:查找“组”字符串
    示例:“egrep -n ‘t(a|e)st’ test.txt”。“tast”与“test”因为这两个单词的“t”与“st”是重复的,所以将“a”与“e” 列于“()”符号当中,并以“|”分隔,即可查询"tast"或者"test"字符串
  • ()+ 作用:辨别多个重复的组
    示例:“egrep -n ‘A(xyz)+C’ test.txt”。该命令是查询开头的"A"结尾是"C",中间有一个以上的"xyz"字符串的意思
  • {n} n 是一个非负整数,匹配确定的 n 次。例如,“o{2}”不能匹配“Bob”中的“o”,但是能匹配“food”中的“oo”
  • {n,} n 是一个非负整数,至少匹配 n 次。例如,“o{2,}”不能匹配“Bob”中的“o”,但能匹配“foooood”中的所有 o。“o{1,}”等价于“o+”。“o{0,}”则等价于“o*”
  • {n,m} m 和 n 均为非负整数,其中 n<=m,最少匹配 n 次且最多匹配 m 次

查找连续字符范围“{}”
提示:注意如果使用grep的话要加转义符”\“,例如:grep -n 'o\{2\}' test.txt
查询两个 o 的字符。

[root@localhost ~]# egrep -n 'o{2}' test.txt
3:The home of Football on BBC Sport online. 5:google is the best tools for search keyword. 8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!

查询以 w 开头以 d 结尾,中间包含 2~5 个 o 的字符串。

[root@localhost ~]# egrep -n 'wo{2,5}d' test.txt
8:a wood cross!
11:#woood #

查询以 w 开头以 d 结尾,中间包含 2 个或 2 个以上 o 的字符串。

[root@localhost ~]# egrep -n 'wo{2,}d' test.txt
8:a wood cross!
11:#woood #
12:#woooooood #

总结

例如:正则就是用来匹配字符串的,分为基础正则,和扩展正则。


  1. a-z ↩︎

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