10.1.5 Specifying Match Position
As described earlier, many elements of a regular expression match a single character in a string. For example, \s matches a single character of whitespace. Other regular expression elements match the positions between characters, instead of actual characters. \b, for example, matches a word boundary—the boundary between a \w (ASCII word character) and a \W (nonword character), or the boundary between an ASCII word character and the beginning or end of a string.2 Elements such as \b do not specify any
2. Except within a character class (square brackets), where \b matches the backspace character.
10.1 Defining Regular Expressions | 257
characters to be used in a matched string; what they do specify, however, are legal positions at which a match can occur. Sometimes these elements are called regular-expression anchors because they anchor the pattern to a specific position in the search string. The most commonly used anchor elements are ^, which ties the pattern to the beginning of the string, and $, which anchors the pattern to the end of the string.
Table 10-5 summarizes regular-expression anchors.
Table 10-5. Regular-expression anchor characters
|^||Match the beginning of the string and, in multiline searches, the beginning of a line.|
|$||Match the end of the string and, in multiline searches, the end of a line.|
|\b||Match a word boundary. That is, match the position between a \wcharacter and a \Wcharacter or between a|
|\wcharacter and the beginning or end of a string. (Note, however, that [\b]matches backspace.)|
|\B||Match a position that is not a word boundary.|
|(?=p)||A positive lookahead assertion. Require that the following characters match the pattern p, but do not include|
|those characters in the match.|
|(?!p)||A negative lookahead assertion. Require that the following characters do not match the pattern p.|
258 | Chapter 10: Pattern Matching with Regular Expressions欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/31396.html