正则表达式最佳教程3(4)

2019-02-15 11:13

I found the text \starting at index 0 and ending at index 1.

Enter your regex: \\w

Enter input string to search: ! No match found.

Enter your regex: \\W

Enter input string to search: a No match found.

Enter your regex: \\W

Enter input string to search: !

I found the text \starting at index 0 and ending at index 1.

在开始的三个例子中,正则表达式是简单的,.(“点”元字符)表示“任意字符”,因此,在所有的三个

例子(随意地选取了“@”字符,数字和字母)中都是匹配成功的。在接下来的例子中,都使用了预定义字符类表格中的单个正则表达式构造。你应该可以根据这张表指出前面每个匹配的逻辑:

\\d 匹配数字字符 \\s 匹配空白字符 \\w 匹配单词字符 \\D 匹配非数字字符 \\S 匹配非空白字符 \\W 匹配非单词字符

也可以使用意思正好相反的大写字母:

5 量词返回目录

这一节我们来看一下贪婪(greedy)、勉强(reluctant)和侵占(possessive)量词,来匹配指定表达式X的次数。

量词(quantifiers)允许指定匹配出现的次数,方便起见,当前 Pattern API 规范下,描述了贪婪、勉强和侵占三种量词。首先粗略地看一下,量词X?、X??和X?+都允许匹配 X 零次或一次,精确地做同样的事情,但它们之间有着细微的不同之处,在这节结束前会进行说明。

量 词 种 类 贪婪

勉强

侵占

意 义

匹配 X 零次或一次 X? X?? X?+

匹配 X 零次或多次 X* X*? X*+

匹配 X 一次或多次 X+ X+? X++

X{n} X{n}? X{n}+ 匹配 X n 次

X{n,} X{n,}? X{n,}+ 匹配 X 至少 n 次 X{n,m} X{n,m}? X{n,m}+匹 配 X 至少 n 次,但不多于 m 次

那我们现在就从贪婪量词开始,构建三个不同的正则表达式:字母a后面跟着?、*和+。接下来看一

下,用这些表达式来测试输入的字符串是空字符串时会发生些什么:

Enter your regex: a?

Enter input string to search:

I found the text \0.

Enter your regex: a*

Enter input string to search:

I found the text \0.

Enter your regex: a+

Enter input string to search: No match found.

5.1 零长度匹配返回目录

在上面的例子中,开始的两个匹配是成功的,这是因为表达式a?和a*都允许字符出现零次。就目前而言,这个例子不像其他的,也许你注意到了开始和结束的索引都是 0。输入的空字符串没有长度,因此该测试简单地在索引 0 上匹配什么都没有,诸如此类的匹配称之为零长度匹配(zero-length matches)。零长度匹配会出现在以下几种情况:输入空的字符串、在输入字符串的开始处、在输入字符串最后字符的后面,或者是输入字符串中任意两个字符之间。由于它们开始和结束的位置有着相同的索引,因此零长度匹配是容易被发现的。

我们来看一下关于零长度匹配更多的例子。把输入的字符串改为单个字符“a”,你会注意到一些有意思的事情:

Enter your regex: a?

Enter input string to search: a

I found the text \starting at index 0 and ending at index 1.

I found the text \1.

Enter your regex: a*

Enter input string to search: a

I found the text \starting at index 0 and ending at index 1.

I found the text \1.

Enter your regex: a+

Enter input string to search: a

I found the text \starting at index 0 and ending at index 1.

所有的三个量词都是用来寻找字母“a”的,但是前面两个在索引 1 处找到了零长度匹配,也就是说,在输入字符串最后一个字符的后面。回想一下,匹配把字符“a”看作是位于索引 0 和索引 1 之间的单元格中,并且测试用具一直循环下去直到不再有匹配为止。依赖于所使用的量词不同,最后字符后面的索引“什么也没有”的存在可以或者不可以触发一个匹配。

现在把输入的字符串改为一行 5 个“a”时,会得到下面的结果:

Enter your regex: a?

Enter input string to search: aaaaa

I found the text \starting at index 0 and ending at index 1.

I found the text \starting at index 1 and ending at index 2.

I found the text \starting at index 2 and ending at index 3.

I found the text \starting at index 3 and ending at index 4.

I found the text \starting at index 4 and ending at index 5.

I found the text \5.

Enter your regex: a*

Enter input string to search: aaaaa

I found the text \index 5.

I found the text \5.

Enter your regex: a+

Enter input string to search: aaaaa

I found the text \index 5.

在“a”出现零次或一次时,表达式a?寻找到所匹配的每一个字符。表达式a*找到了两个单独的匹配:第一次匹配到所有的字母“a”,然后是匹配到最后一个字符后面的索引 5。最后,a+匹配了所有出现的字母

“a”,忽略了在最后索引处“什么都没有”的存在。

在这里,你也许会感到疑惑,开始的两个量词在遇到除了“a”的字母时会有什么结果。例如,在“ababaaaab”中遇到了字母“b”会发生什么呢? 下面我们来看一下:

Enter your regex: a?

Enter input string to search: ababaaaab

I found the text \starting at index 0 and ending at index 1.

I found the text \1.

I found the text \starting at index 2 and ending at index 3.

I found the text \3.

I found the text \starting at index 4 and ending at index 5.

I found the text \starting at index 5 and ending at index 6.

I found the text \starting at index 6 and ending at index 7.

I found the text \starting at index 7 and ending at index 8.

I found the text \8.

I found the text \9.

Enter your regex: a*

Enter input string to search: ababaaaab

I found the text \starting at index 0 and ending at index 1.

I found the text \1.

I found the text \starting at index 2 and ending at index 3.

I found the text \3.

I found the text \starting at index 4 and ending at index 8.

I found the text \8.

I found the text \9.

Enter your regex: a+

Enter input string to search: ababaaaab

I found the text \starting at index 0 and ending at index 1.

I found the text \starting at index 2 and ending at index 3.

I found the text \starting at index 4 and ending at index 8.

即使字母“b”在单元格 1、3、8 中出现,但在这些位置上的输出报告了零长度匹配。正则表达式a?不

是特意地去寻找字母“b”,它仅仅是去找字母“a”存在或者其中缺少的。如果量词允许匹配“a”零次,任何输入的字符不是“a”时将会作为零长度匹配。在前面的例子中,根据讨论的规则保证了 a 被匹配。 对于要精确地匹配一个模式 n 次时,可以简单地在一对花括号内指定一个数值:

Enter your regex: a{3}

Enter input string to search: aa No match found.

Enter your regex: a{3}

Enter input string to search: aaa

I found the text \starting at index 0 and ending at index 3.

Enter your regex: a{3}

Enter input string to search: aaaa

I found the text \starting at index 0 and ending at index 3.

这里,正则表确定式a{3}在一行中寻找连续出现三次的字母“a”。第一次测试失败的原由在于,输入的字符串没有足够的 a 用来匹配;第二次测试输出的字符串正好包括了三个“a”,触发了一次匹配;第三次测试也触发了一次匹配,这是由于在输出的字符串的开始部分正好有三个“a”。接下来的事情与第一次的匹配是不相关的,如果这个模式将在这一点后继续出现,那它将会触发接下来的匹配:

Enter your regex: a{3}

Enter input string to search: aaaaaaaaa

I found the text \starting at index 0 and ending at index 3.

I found the text \starting at index 3 and ending at index 6.

I found the text \starting at index 6 and ending at index 9.

对于需要一个模式出现至少 n 次时,可以在这个数字后面加上一个逗号(,): Enter your regex: a{3,}

Enter input string to search: aaaaaaaaa

I found the text \at index 9.

输入一样的字符串,这次测试仅仅找到了一个匹配,这是由于一个中有九个“a”满足了“至少”三个“a”的要求。

最后,对于指定出现次数的上限,可以在花括号添加第二个数字。

Enter your regex: a{3,6} // 寻找一行中至少连续出现 3 个(但不多于 6 个)“a”

Enter input string to search: aaaaaaaaa

I found the text \index 6.

I found the text \starting at index 6 and ending at index 9.

这里,第一次匹配在 6 个字符的上限时被迫终止了。第二个匹配包含了剩余的三个 a(这是匹配所允许最小的字符个数)。如果输入的字符串再少掉一个字母,这时将不会有第二个匹配,之后仅剩余两个 a。 5.2 捕获组和字符类中的量词返回目录

到目前为止,仅仅测试了输入的字符串包括一个字符的量词。实际上,量词仅仅可能附在一个字符后面一次,因此正则表达式abc+的意思就是“a 后面接着 b,再接着一次或者多次的 c”,它的意思并不是指abc一次或者多次。然而,量词也可能附在字符类和捕获组的后面,比如,[abc]+表示一次或者多次的 a 或 b 或 c,(abc)+表示一次或者多次的“abc”组。 我们来指定(dog)组在一行中三次进行说明。

Enter your regex: (dog){3}

Enter input string to search: dogdogdogdogdogdog

I found the text \at index 9.

I found the text \at index 18.

Enter your regex: dog{3}


正则表达式最佳教程3(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:浙大远程教育民法总论在线作业答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: