- 12 minutes to read
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. For a brief introduction, see .NET Regular Expressions.
Each section in this quick reference lists a particular category of characters, operators, and constructs that you can use to define regular expressions.
We've also provided this information in two formats that you can download and print for easy reference:
- Download in Word (.docx) format
- Download in PDF (.pdf) format
The backslash character (\) in a regular expression indicates that the character that follows it either is a special character (as shown in the following table), or should be interpreted literally. For more information, see Character Escapes.
|Matches a bell character, \u0007.|
|In a character class, matches a backspace, \u0008.|
|Matches a tab, \u0009.|
|Matches a carriage return, \u000D. (|
|Matches a vertical tab, \u000B.|
|Matches a form feed, \u000C.|
|Matches a new line, \u000A.|
|Matches an escape, \u001B.|
|Uses octal representation to specify a character (nnn consists of two or three digits).|
|Uses hexadecimal representation to specify a character (nn consists of exactly two digits).|
|Matches the ASCII control character that is specified by X or x, where X or x is the letter of the control character.|
|Matches a Unicode character by using hexadecimal representation (exactly four digits, as represented by nnnn).|
|When followed by a character that is not recognized as an escaped character in this and other tables in this topic, matches that character. For example, |
A character class matches any one of a set of characters. Character classes include the language elements listed in the following table. For more information, see Character Classes.
|Matches any single character in character_group. By default, the match is case-sensitive.|
|Negation: Matches any single character that is not in character_group. By default, characters in character_group are case-sensitive.|
|Character range: Matches any single character in the range from first to last.|
|Wildcard: Matches any single character except \n.|
To match a literal period character (. or
|Matches any single character in the Unicode general category or named block specified by name.|
|Matches any single character that is not in the Unicode general category or named block specified by name.|
|Matches any word character.|
|Matches any non-word character.|
|Matches any white-space character.|
|Matches any non-white-space character.|
|Matches any decimal digit.|
|Matches any character other than a decimal digit.|
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. For more information, see Anchors.
|By default, the match must start at the beginning of the string; in multiline mode, it must start at the beginning of the line.|
|By default, the match must occur at the end of the string or before |
|The match must occur at the start of the string.|
|The match must occur at the end of the string or before |
|The match must occur at the end of the string.|
|The match must occur at the point where the previous match ended, or if there was no previous match, at the position in the string where matching started.|
|The match must occur on a boundary between a |
|The match must not occur on a |
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. For more information, see Grouping Constructs.
|Captures the matched subexpression and assigns it a one-based ordinal number.|
|Captures the matched subexpression into a named group.|
|Defines a balancing group definition. For more information, see the "Balancing Group Definition" section in Grouping Constructs.|
|Defines a noncapturing group.|
|Applies or disables the specified options within subexpression. For more information, see Regular Expression Options.|
|Zero-width positive lookahead assertion.|
|Zero-width negative lookahead assertion.|
|Zero-width positive lookbehind assertion.|
|Zero-width negative lookbehind assertion.|
Lookarounds at a glance
When the regular expression engine hits a lookaround expression, it takes a substring reaching from the current position to the start (lookbehind) or end (lookahead) of the original string, and then runsRegex.IsMatch on that substring using the lookaround pattern. Success of this subexpression's result is then determined by whether it's a positive or negative assertion.
|PositiveLookahead||Asserts that what immediately follows the current position in the string is "check"|
|PositiveLookbehind||Asserts that what immediately precedes the current position in the string is "check"|
|NegativeLookahead||Asserts that what immediately follows the current position in the string is not "check"|
|NegativeLookbehind||Asserts that what immediately precedes the current position in the string is not "check"|
Once they have matched, atomic groups won't be re-evaluated again, even when the remainder of the pattern fails due to the match. This can significantly improve performance when quantifiers occur within the atomic group or the remainder of the pattern.
A quantifier specifies how many instances of the previous element (which can be a character, a group, or a character class) must be present in the input string for a match to occur. Quantifiers include the language elements listed in the following table. For more information, see Quantifiers.
|Matches the previous element zero or more times.|
|Matches the previous element one or more times.|
|Matches the previous element zero or one time.|
|Matches the previous element exactly n times.|
|Matches the previous element at least n times.|
|Matches the previous element at least n times, but no more than m times.|
|Matches the previous element zero or more times, but as few times as possible.|
|Matches the previous element one or more times, but as few times as possible.|
|Matches the previous element zero or one time, but as few times as possible.|
|Matches the preceding element exactly n times.|
|Matches the previous element at least n times, but as few times as possible.|
|Matches the previous element between n and m times, but as few times as possible.|
A backreference allows a previously matched subexpression to be identified subsequently in the same regular expression. The following table lists the backreference constructs supported by regular expressions in .NET. For more information, see Backreference Constructs.
|Backreference. Matches the value of a numbered subexpression.|
|Named backreference. Matches the value of a named expression.|
Alternation constructs modify a regular expression to enable either/or matching. These constructs include the language elements listed in the following table. For more information, see Alternation Constructs.
|Matches any one element separated by the vertical bar (|
|Matches yes if the regular expression pattern designated by expression matches; otherwise, matches the optional no part. expression is interpreted as a zero-width assertion.|
To avoid ambiguity with a named or numbered capturing group, you can optionally use an explicit assertion, like this:
|Matches yes if name, a named or numbered capturing group, has a match; otherwise, matches the optional no.|
Substitutions are regular expression language elements that are supported in replacement patterns. For more information, see Substitutions. The metacharacters listed in the following table are atomic zero-width assertions.
|Character||Description||Pattern||Replacement pattern||Input string||Result string|
|Substitutes the substring matched by group number.|
|Substitutes the substring matched by the named group name.|
|Substitutes a literal "$".|
|Substitutes a copy of the whole match.|
|Substitutes all the text of the input string before the match.|
|Substitutes all the text of the input string after the match.|
|Substitutes the last group that was captured.|
|Substitutes the entire input string.|
Regular Expression Options
You can specify options that control how the regular expression engine interprets a regular expression pattern. Many of these options can be specified either inline (in the regular expression pattern) or as one or more RegexOptions constants. This quick reference lists only inline options. For more information about inline and RegexOptions options, see the article Regular Expression Options.
You can specify an inline option in two ways:
- By using the miscellaneous construct
(?imnsx-imnsx), where a minus sign (-) before an option or set of options turns those options off. For example,
(?i-mn)turns case-insensitive matching (
i) on, turns multiline mode (
m) off, and turns unnamed group captures (
n) off. The option applies to the regular expression pattern from the point at which the option is defined, and is effective either to the end of the pattern or to the point where another construct reverses the option.
- By using the grouping construct
), which defines options for the specified group only.
The .NET regular expression engine supports the following inline options:
|Use case-insensitive matching.|
|Use multiline mode. ||For an example, see the "Multiline Mode" section in Regular Expression Options.|
|Do not capture unnamed groups.||For an example, see the "Explicit Captures Only" section in Regular Expression Options.|
|Use single-line mode.||For an example, see the "Single-line Mode" section in Regular Expression Options.|
|Ignore unescaped white space in the regular expression pattern.|
Miscellaneous constructs either modify a regular expression pattern or provide information about it. The following table lists the miscellaneous constructs supported by .NET. For more information, see Miscellaneous Constructs.
|Sets or disables options such as case insensitivity in the middle of a pattern.For more information, see Regular Expression Options.|
|Inline comment. The comment ends at the first closing parenthesis.|
|X-mode comment. The comment starts at an unescaped |
- Regular Expressions
- Regular Expression Classes
- Regular Expressions - Quick Reference (download in Word format)
- Regular Expressions - Quick Reference (download in PDF format)