Regular Expression Anchors .NET Anchors Start of String or Line: ^ End of String or Line: $ Start of String Only: \A End of String or Before Ending Newline: \Z End of String Only: \z Contiguous Matches: \G Word Boundary: \b Non-Word Boundary: \B Examples See also Source/Reference
Regular Expression Anchors
Anchors, or atomic zero-width assertions, specify a position in the string where a match must occur. When using an anchor in a search expression, the regular expression engine does not advance through the string or consume characters; it looks for a match in the specified position only.
a symbol is used to represent an anchor. e.g. ^, $.
an escaped character is used to represent an anchor, e.g. \A, \Z, \z, \G, \b, and \B.
.NET Anchors
Start of String or Line: ^
By default, the ^ anchor is used to specify that the following pattern must begin at the first character position of the string. If the ^ anchor is used with the RegexOptions.Multiline option, the match must occur at the beginning of each line.
End of String or Line: $
By default, the $ anchor is used to specify that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string. If the $ anchor is used with the RegexOptions.Multiline option, the match can also occur at the end of a line. Note that $ matches \n but does not match \r\n (the combination of carriage return and newline characters, or CR/LF). To match the CR/LF character combination, include \r?$ in the regular expression pattern.
Start of String Only: \A
The escaped \A anchor is used specify that the following pattern must occur at the beginning of the input string. It is identical to the ^ anchor, except that \A ignores the RegexOptions.Multiline option. Therefore, it can only match the start of the first line in a multiline input string.
End of String or Before Ending Newline: \Z
The escaped \Z anchor is used to specify that the preceding must occur at the end of the input string, or before \n at the end of the input string. It is identical to the $ anchor, except that \Z ignores the RegexOptions.Multiline option. Therefore, in a multiline string, it can only match the end of the last line, or the last line before \n.
Note that \Z matches \n but does not match \r\n (the CR/LF character combination). To match CR/LF, include \r?\Z in the regular expression pattern.
End of String Only: \z
The escaped \z anchor is used to specify that the preceding pattern must occur at the end of the input string. Like the $ language element, \z ignores the RegexOptions.Multiline option. Unlike the \Z language element, \z does not match a \n character at the end of a string. Therefore, it can only match the last line of the input string.
Contiguous Matches: \G
The escaped \G anchor is used to specify that the following pattern must occur at the point where the previous match ended. When you use this anchor with the Regex.Matches or Match.NextMatch method, it ensures that all matches are contiguous.
Word Boundary: \b
The escaped \b anchor is used to specify that the match must occur on a boundary between a word character (the \w language element) and a non-word character (the \W language element). Word characters consist of alphanumeric characters and underscores; a non-word character is any character that is not alphanumeric or an underscore. (For more information, see Character Classes.) The match may also occur on a word boundary at the beginning or end of the string.
The \b anchor is frequently used to ensure that a subexpression matches an entire word instead of just the beginning or end of a word.
Non-Word Boundary: \B
The escaped \B anchor is used to specify that the match must not occur on a word boundary. It is the opposite of the \b anchor.