Grep 命令中的高级正则表达式与 10 个示例 - 第二部分

举报
Tiamo_T 发表于 2021/11/11 09:29:10 2021/11/11
【摘要】 在我们之前的正则表达式第 1 部分文章中,我们通过实例回顾了基本的操作。 但是我们可以用正则表达式做更多的事情。您通常可以使用单个正则表达式完成复杂的任务,而无需编写多行代码。

在我们之前的正则表达式第 1 部分文章中,我们通过实例回顾了基本的操作。

但是我们可以用正则表达式做更多的事情。您通常可以使用单个正则表达式完成复杂的任务,而无需编写多行代码。

将正则表达式应用于字符串时,正则表达式引擎将从字符串的第一个字符开始。它将在第一个字符处尝试正则表达式的所有可能排列。只有在尝试了所有可能性并发现失败时,正则表达式引擎才会继续使用文本中的第二个字符。

正则表达式会以完全相同的顺序尝试所有可能的正则表达式排列。结果是正则表达式导向的引擎将返回最左边的匹配项。

在本文中,让我们通过示例来回顾一些高级正则表达式。

示例 1. OR 运算 (|)

grep 中的管道符 (|) 用于指定两个完整子表达式中的任何一个出现在一个位置。“subexpression1|subexpression2”匹配subexpression1 或subexpression2。

以下示例将在 grep 命令中使用 OR 删除文件中三种不同类型的注释行。

首先,创建一个名为“comments”的示例文件。


$ cat comments
This file shows the comment character in various programming/scripting languages
### Perl / shell scripting
If the Line starts with single hash symbol,
then its a comment in Perl and shell scripting.
' VB Scripting comment
The line should start with a single quote to comment in VB scripting.
// C programming single line comment.
Double slashes in the beginning of the line for single line comment in C.

名为“comments”的文件有 perl、VB 脚本和 C 编程注释行。现在,以下 grep 命令搜索不以 # 或单引号 (') 或双斜线 (//) 开头的行。

$ grep  -v "^#\|^'\|^\/\/" comments
This file shows the comment character in various programming/scripting languages
If the Line starts with single hash symbol,
then its a comment in Perl and shell scripting.
The line should start with a single quote to comment in VB scripting.
Double slashes in the beginning of the line for single line comment in C.

示例 2. 字符类表达式

正如我们在之前的正则表达式文章示例 9 中所见,可以在方括号中提及字符列表以仅匹配多个字符中的一个。Grep 命令支持一些表示某些常见范围的特殊字符类。这里列出了其中的几个。请参阅 grep 的手册页以了解各种字符类表达式。

[:digit:] 	Only the digits 0 to 9
[:alnum:] 	Any alphanumeric character 0 to 9 OR A to Z or a to z.
[:alpha:] 	Any alpha character A to Z or a to z.
[:blank:] 	Space and TAB characters only.

它们总是以 [[:digit:]] 的形式在方括号内使用。现在让我们使用适当的字符类表达式 grep ntpd 守护进程的所有进程 ID。

$ grep -e "ntpd\[[[:digit:]]\+\]" /var/log/messages.4
Oct 28 11:42:20 gstuff1 ntpd[2241]: synchronized to LOCAL(0), stratum 10
Oct 28 11:42:20 gstuff1 ntpd[2241]: synchronized to 15.11.13.123, stratum 3
Oct 28 12:33:31 gstuff1 ntpd[2241]: synchronized to LOCAL(0), stratum 10
Oct 28 12:50:46 gstuff1 ntpd[2241]: synchronized to 15.11.13.123, stratum 3
Oct 29 07:55:29 gstuff1 ntpd[2241]: time reset -0.180737 s

示例 3. M 到 N 次出现 ({m,n})

正则表达式后跟 {m,n} 表示前一项至少匹配 m 次,但不超过 n 次。m 和 n 的值必须为非负且小于 255。

如果该行在 0 到 99999 的范围内,以下示例将打印该行。

$ cat  number
12
12345
123456
19816282

$ grep  "^[0-9]\{1,5\}$" number
12
12345

名为“number”的文件有数字列表,上面的grep命令只匹配1(最小为0)到5位(最大99999)的数字。

示例 4. 精确 M 出现 ({m})

后跟 {m} 的正则表达式正好匹配前面表达式的 m 次出现。以下 grep 命令将仅显示具有 5 位数字的数字。

$ grep  "^[0-9]\{5\}$" number
12345

示例 5. M 次或多次出现 ({m,})

后跟 {m,} 的正则表达式匹配前面表达式的 m 次或多次出现。以下 grep 命令将显示具有 5 位或更多位的数字。

$ grep "[0-9]\{5,\}" number
12345
123456
19816282

示例 6. 字边界 (\b)

\b 是匹配单词边界。\b 匹配单词开头 (\bxx) 和/或结尾 (xx\b) 的任何字符,因此 \bthe\b 会找到 the 而不是 thet,但 \bthe 会找到它们。

# grep -i "\bthe\b" comments
This file shows the comment character in various programming/scripting languages
If the Line starts with single hash symbol,
The line should start with a single quote to comment in VB scripting.
Double slashes in the beginning of the line for single line comment in C.

示例 7. 反向引用 (\n)

可以通过反向引用在 grep 中对表达式进行分组以供进一步使用。例如,\([0-9]\)\1 匹配两位数字相同的数字,如 11,22,33 等,

# grep -e '^\(abc\)\1$'
abc
abcabc
abcabc

在上面的 grep 命令中,它接受 STDIN 的输入。当它读取不匹配的输入“abc”时,“abcabc”行与给定的表达式匹配,因此它会打印出来。如果你想使用扩展正则表达式,它总是首选使用 egrep 命令。带 -e 选项的 grep 也像 egrep 一样工作,但你必须转义像括号这样的特殊字符。

注意:您也可以使用zgrep 命令在压缩的 gz 文件中进行搜索。

示例 8. 匹配模式“Object Oriented”

到目前为止,我们已经在 grep 命令中看到了不同的提示,现在使用这些提示,让我们匹配各种格式的“面向对象”。

$ grep "OO\|\([oO]bject\( \|\-\)[oO]riented\)"

上面的grep命令匹配“OO”、“面向对象”、“面向对象”等,

示例 9. 打印“vowel singlecharacter samevowel”这一行

以下 grep 命令打印所有包含元音(a、e、i、o 或 u)后跟单个字符后跟相同元音的行。因此,它会找到 eve 或 adam,但不会找到 vera。

$ cat input
evening
adam
vera

$ grep "\([aeiou]\).\1" input
evening
adam

示例 10. 有效的 IP 地址

以下 grep 命令仅匹配有效的 IP 地址。

$ cat input
15.12.141.121
255.255.255
255.255.255.255
256.125.124.124

$ egrep  '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' input
15.12.141.121
255.255.255.255

在上面给出的正则表达式中,有不同的条件。这些有条件的匹配应该出现 3 次,另外一个类被单独提及。

  1. 如果以 25 开头,则下一个数字应为 0 到 5(250 到 255)
  2. 如果以 2 开头,则下一个数字可能是 0-4,然后是 0-9(200 到 249)
  3. 0 或 1、0-9 的零出现,然后 0-9(0 到 199)之间的任何数字零的出现
  4. 点阵字符 
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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

举报
请填写举报理由
0/200