A regular expression is a pattern that the regular expression engine attempts to match in input text. A pattern consists of one or more character literals, operators, or constructs.
Anchors
Anchors, or atomic zero-width assertions, cause a match to succeed or fail depending on the current position in the string, but they do not cause the engine to advance through the string or consume characters. The metacharacters listed in the following table are anchors.
Assertion | Description | Pattern | Matches |
---|---|---|---|
^ | The match must start at the beginning of the string or line. | ^\d{3} | "901" in "901-333-" |
$ | The match must occur at the end of the string or before \n at the end of the line or string. | -\d{3}$ | "-333" in "-901-333" |
\A | The match must occur at the start of the string. | \A\d{3} | "901" in "901-333-" |
\Z | The match must occur at the end of the string or before \n at the end of the string. | -\d{3}\Z | "-333" in "-901-333" |
\z | The match must occur at the end of the string. | -\d{3}\z | "-333" in "-901-333" |
\G | The match must occur at the point where the previous match ended. | \G\(\d\) | "(1)", "(3)", "(5)" in "(1)(3)(5)[7](9)" |
\b | The match must occur on a boundary between a \w (alphanumeric) and a \W (nonalphanumeric) character. | \b\w+\s\w+\b | "them theme", "them them" in "them theme them them" |
\B | The match must not occur on a \b boundary. | \Bend\w*\b | "ends", "ender" in "end sends endure lender" |
Grouping Constructs
Grouping constructs delineate subexpressions of a regular expression and typically capture substrings of an input string. Grouping constructs include the language elements listed in the following table.
Grouping construct | Description | Pattern | Matches |
---|---|---|---|
( subexpression ) | Captures the matched subexpression and assigns it a one-based ordinal number. | (\w)\1 | "ee" in "deep" |
(?< name > subexpression) | Captures the matched subexpression into a named group. | (?<double>\w)\k<double> | "ee" in "deep" |
(?< name1 - name2 > subexpression) | Defines a balancing group definition. For more information, see the "Balancing Group Definition" section in Grouping Constructs in Regular Expressions. | (((?'Open'\()[^\(\)]*)+((?'Close-Open'\))[^\(\)]*)+)*(?(Open)(?!))$ | "((1-3)*(3-1))" in "3+2^((1-3)*(3-1))" |
(?: subexpression) | Defines a noncapturing group. | Write(?:Line)? | "WriteLine" in "Console.WriteLine()" "Write" in "Console.Write(value)" |
(?imnsx-imnsx: subexpression) | Applies or disables the specified options within subexpression. | A\d{2}(?i:\w+)\b | "A12xl", "A12XL" in "A12xl A12XL a12xl" |
(?= subexpression) | Zero-width positive lookahead assertion. | \w+(?=\.) | "is", "ran", and "out" in "He is. The dog ran. The sun is out." |
(?! subexpression) | Zero-width negative lookahead assertion. | \b(?!un)\w+\b | "sure", "used" in "unsure sure unity used" |
(?<= subexpression) | Zero-width positive lookbehind assertion. | (?<=19)\d{2}\b | "99", "50", "05" in "1851 1999 1950 1905 2003" |
(?<! subexpression) | Zero-width negative lookbehind assertion. | (?<!19)\d{2}\b | "51", "03" in "1851 1999 1950 1905 2003" |
(?> subexpression) | Nonbacktracking (or "greedy") subexpression. | [13579](?>A+B+) | "1ABB", "3ABB", and "5AB" in "1ABB 3ABBC 5AB 5AC" |
No comments:
Post a Comment