13.8 边界匹配器
到目前为止,我们只对是否在特定输入字符串的某个位置找到匹配感兴趣。我们从未关心过匹配发生在什么位置。
通过使用边界匹配器(boundary matcher)指定这样的信息,可以使模式匹配更加精确。例如,可能你希望找到特定单词,但是只搜索它出现在一行的开头或者末尾的情况。或者可能你希望知道匹配是否出现在单词边界或者前一个匹配的末尾。
表13-4列出并且解释所有边界匹配器。
表13-4 边界匹配器
|
^ |
一行的开头 |
|
$ |
一行的末尾 |
|
\b |
单词边界 |
|
\B |
非单词边界 |
|
\A |
输入的开头 |
|
\G |
前一个匹配的末尾 |
|
\Z |
输入的末尾,但是只用于最后的终止符(如果有的话) |
|
\z |
输入的末尾 |
下面的例子演示边界匹配器^和$。前面讲过,^匹配一行的开头,而$匹配末尾:
Enter your regex: ^dog$
Enter input string to search: dog
I found the text "dog" starting at index 0 and ending at index 3.
Enter your regex: ^dog$
Enter input string to search: dog
No match found.
Enter your regex: \s*dog$
Enter input string to search: dog
I found the text " dog" starting at index 0 and ending at index 15.
Enter your regex: ^dog\w*
Enter input string to search: dogblahblah
I found the text "dogblahblah" starting at index 0 and ending at index 11.
第一个例子成功是因为模式占据了整个输入字符串。第二个例子失败,因为输入字符串的开头包含额外空白。第三个例子指定的表达式允许数量不限的空白,后面在行的末尾是“dog”。第四个例子要求“dog”出现在行的开头,后面跟着数量不限的单词字符。
为了检查模式是否在单词边界开始或者结束(而不是更长的字符串内的子字符串),只需在模式的一边加上\b;例如\bdog\b:
Enter your regex: \bdog\b
Enter input string to search: The dog plays in the yard.
I found the text "dog" starting at index 4 and ending at index 7.
Enter your regex: \bdog\b
Enter input string to search: The doggie plays in the yard.
No match found.
为了在非单词边界匹配表达式,使用\B:
Enter your regex: \bdog\B
Enter input string to search: The dog plays in the yard.
No match found.
Enter your regex: \bdog\B
Enter input string to search: The doggie plays in the yard.
I found the text "dog" starting at index 4 and ending at index 7.
为了要求匹配只出现在前一个匹配的末尾,使用\G:
Enter your regex: dog
Enter input string to search: dog dog
I found the text "dog" starting at index 0 and ending at index 3.
I found the text "dog" starting at index 4 and ending at index 7.
Enter your regex: \Gdog
Enter input string to search: dog dog
I found the text "dog" starting at index 0 and ending at index 3.
第二个例子只找到一个匹配,因为“dog”的第二次出现不是从前一个匹配的末尾开始的。






