通过本文的学习,您可以增加一些有用的设计实际正则表达式 (regexp) 的技能。构建正则表达式是任何管理员日常工作中的一部分。为了构造返回所需条件的成功正则表达式,需要学习以模式匹配的角度进行思考,而这种技能需要花大量的时间进行练习。
引言
UNIX® 管理员每天都需要构建和使用正则表达式 (regexp) 进行文本模式匹配。大多数语言都支持正则表达式的某种实现。有的应用程序(如 EMACS)具有正则表达式搜索功能,并且您可以通过各种命令行工具使用正则表达式。无论什么应用程序,构建正确的正则表达式的关键之处在于,识别仅满足需要匹配的数据的模式,以便在输入中排除其他不必要的内容。
出于这个目的,本文将逐步介绍几种正则表达式模式构建技巧,并介绍它们如何帮助您完成各种常规任务。
使用正则表达式 (regexp)
除非特别说明,否则本文中使用的示例都是扩展可移植操作系统接口(扩展 POSIX)的正则表达式。如果通过命令行(如使用 egrep 实用工具)使用它们,您应该根据需要引用各种正则表达式。请记住,不同的正则表达式实现之间存在一些区别,您可能不得不适应所使用的特定的工具、应用程序或语言中的具体实现。
匹配整行内容
^ 元字符匹配行首,而 $ 匹配行尾,如果将它们组合在一起(如 ^$),它们将匹配空行。(这个表达式的镜像,即 $^,是不可能匹配成功的,它将永远 都无法匹配到有效行。)这个基本的正则表达式是许多复杂正则表达式的基础,如果您还不习惯使用这个基本的正则表达式,那么您应该逐步养成使用它的习惯。使用它来构建匹配整行内容 的模式。
在用户字典文件 (/usr/dict/words) 中搜索是一个很好的基本模式。(有些版本的 UNIX 将用户字典放在 /usr/share/dict/words 中。)
例如,假设您忘记了如何拼写单词 fuchsia。其中是否包含 sh 或 cs 呢?您所知道的只是,它以 fu 开头并以 ia 结尾。
尝试使用这个模式进行搜索:
$ egrep -i '^fu.*ia$' /usr/dict/words
|
-i 标志表示在搜索过程中不区分大小写。在这个示例中,因为 fuchsia 拼写正确,所以在返回的单词中包括这个单词。
根据长度匹配行
使用大括号元字符 ({ }) 指定前面的正则表达式匹配多少次,如表 1 所示。当您将它们添加到刚才介绍的整行搜索中时,您可以指定行的长度。
表 1. 大括号元字符的含义
示例 |
描述 |
{X} |
这个字符对前面的正则表达式匹配 X 次。 |
{X,} |
这个字符对前面的正则表达式匹配 X 或更多 次。 |
{X,Y} |
这个字符对前面的正则表达式匹配至少 X 而不超过 Y 次。 |
并不是所有扩展正则表达式的实现都支持大括号。此外,根据具体的实现,您可能需要先使用反斜杠对其进行转义。
您可以使用这个正则表达式得到字典中以单词长度为顺序的报告。所获得结果的数目取决于本地系统的字典文件中单词的数目,然而,它应该与清单 1 所示类似。在这个示例中,最常见的单词长度是 9 个字母,该字典中有 32,380 个匹配单词。该字典中不包括 25 个字母或更长的单词,并且最长的单词并不是您认为的 21 个字母长的 disestablishmentarian(有 81 个同样长度的单词,包括 superincomprehensible 和 phoneticohieroglyphic),这个 UNIX 字典中最长的单词有 5 个,包括 pathologicopsychological。
清单 1. 计算字典中 X 个字母的单词的个数
$ for i in `seq 1 32`
> {
> echo "There are" `egrep '^.{'$i'}$' /usr/dict/words \
| wc -l` "$i-letter words in the dictionary."
> }
There are 52 1-letter words in the dictionary.
There are 155 2-letter words in the dictionary.
There are 1351 3-letter words in the dictionary.
There are 5110 4-letter words in the dictionary.
There are 9987 5-letter words in the dictionary.
There are 17477 6-letter words in the dictionary.
There are 23734 7-letter words in the dictionary.
There are 29926 8-letter words in the dictionary.
There are 32380 9-letter words in the dictionary.
There are 30867 10-letter words in the dictionary.
There are 26011 11-letter words in the dictionary.
There are 20460 12-letter words in the dictionary.
There are 14938 13-letter words in the dictionary.
There are 9762 14-letter words in the dictionary.
There are 5924 15-letter words in the dictionary.
There are 3377 16-letter words in the dictionary.
There are 1813 17-letter words in the dictionary.
There are 842 18-letter words in the dictionary.
There are 428 19-letter words in the dictionary.
There are 198 20-letter words in the dictionary.
There are 82 21-letter words in the dictionary.
There are 41 22-letter words in the dictionary.
There are 17 23-letter words in the dictionary.
There are 5 24-letter words in the dictionary.
There are 0 25-letter words in the dictionary.
There are 0 26-letter words in the dictionary.
There are 0 27-letter words in the dictionary.
There are 0 28-letter words in the dictionary.
There are 0 29-letter words in the dictionary.
There are 0 30-letter words in the dictionary.
There are 0 31-letter words in the dictionary.
There are 0 32-letter words in the dictionary.
$
|
[1] [2] [3] 下一页
共7页: 上一页 1 [2] [3] [4] [5] [6] [7] 下一页