The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Until now, we've only been interested in whether or not a match is found at some location within a particular input string. 到目前为止,我们只对是否在特定输入字符串中的某个位置找到匹配项感兴趣。We never cared about where in the string the match was taking place.我们从不在乎匹配在什么地方发生。
You can make your pattern matches more precise by specifying such information with boundary matchers. 通过使用边界匹配器指定此类信息,可以使模式匹配更加精确。For example, maybe you're interested in finding a particular word, but only if it appears at the beginning or end of a line. Or maybe you want to know if the match is taking place on a word boundary, or at the end of the previous match.例如,您可能对查找某个特定单词感兴趣,但前提是该单词出现在一行的开头或结尾。或者,您可能想知道匹配是在单词边界上进行的,还是在上一个匹配的末尾进行的。
The following table lists and explains all the boundary matchers.下表列出并解释了所有边界匹配器。
^ |
|
$ |
|
\b |
|
\B |
|
\A |
|
\G |
|
\Z |
|
\z |
The following examples demonstrate the use of boundary matchers 以下示例演示了边界匹配器^
and $
. ^
和$
的使用。As noted above, 如上所述,^
matches the beginning of a line, and $
matches the end.^
匹配行首,$
匹配行尾。
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.
The first example is successful because the pattern occupies the entire input string. 第一个示例是成功的,因为模式占用了整个输入字符串。The second example fails because the input string contains extra whitespace at the beginning. 第二个示例失败,因为输入字符串的开头包含额外的空格。The third example specifies an expression that allows for unlimited white space, followed by "dog" on the end of the line. 第三个示例指定了一个表达式,该表达式允许无限的空白,在行尾后跟“dog”。The fourth example requires "dog" to be present at the beginning of a line followed by an unlimited number of word characters.第四个示例要求“dog”出现在一行的开头,后跟无限数量的单词字符。
To check if a pattern begins and ends on a word boundary (as opposed to a substring within a longer string), just use 要检查模式是否在单词边界上开始和结束(与较长字符串中的子字符串相反),只需在任意一侧使用\b
on either side; for example, \bdog\b
\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.
To match the expression on a non-word boundary, use 要匹配非单词边界上的表达式,请改用\B
instead:\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.
To require the match to occur only at the end of the previous match, use 要要求仅在上一次匹配结束时进行匹配,请使用\G
:\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.
Here the second example finds only one match, because the second occurrence of "dog" does not start at the end of the previous match.在这里,第二个示例只找到一个匹配项,因为第二次出现的“dog”不是从上一个匹配项的末尾开始的。