The following expressions will match single characters.
| Pattern | Description |
|---|---|
| . | Matches any character excluding newline. |
| [abc] | A Square bracket expression matches any character (with no specific order) that is contained within the brackets. |
| [^abc] | Matches all characters not contained within the square brackets. |
| [a-z] or [0-9] | Matches any single character in the range from first to last. |
| \w | Matches an alpha-numeric character (a-z, A-Z, 0-9, and underscore). |
| \W | The opposite of \w. Matches any non-alphanumeric character. |
| \d | Matches a decimal character (0-9). |
| \D | The opposite of \d. Matches any non-decimal character. |
| \s | Matches a character of whitespace (space, tab, carriage return, line feed). |
| \S | The opposite of \s. Matches any non-whitespace character. |
| \r | Matches a carriage return. |
| \n | Matches a new line (line feed). |
| \f | Matches a form feed. |
| \t | Matches a tab. |
| \v | Matches a vertical tab. |
| \a | Matches a bell character. |
| \b | In a character class, matches a backspace. |
| \e | Matches an escape. |
| \ | In front of any of the special characters (. $ ^ { [ ( | ) * + ? \), ex. \. would match dot character. |
The following expressions specify the location to search for a match, but do not match anything themselves.
| Pattern | Description |
|---|---|
| ^ | The match must start at the beginning of the string (or beginning of the line in multiline mode). |
| $ | The match must occur at the end of the string or before \n at the end of the string (or end of the line in multiline mode). |
| \A | The match must occur at the start of the string. |
| \Z | The match must occur at the end of the string or before \n at the end of the string. |
| \z | The match must occur at the end of the string. |
| \G | The match must occur at the point where the previous match ended. |
| \b | Asserts a boundary between word and non-word characters. |
| \B | The opposite of \b. Asserts a location that is not a boundary between word and non-word characters. |
| (?=pattern) | Asserts that the specified pattern exists immediately after this location. Known as a positive lookahead. |
| (?!pattern) | Asserts that the specified pattern does not exist immediately after this location. Known as a negative lookahead. |
| (?<=pattern) | Asserts that the specified pattern exists immediately before this location. Known as a positive lookbehind. |
| (?<!pattern) | Asserts that the specified pattern does not exist immediately before this location. Known as a negative lookbehind. |
The following expressions will indicate a repetition of the previous character or group.
| Pattern | Description |
|---|---|
| ? | Repeat 0 or 1 time matching as many times as possible. |
| * | Repeat 0 or more times matching as many times as possible. |
| + | Repeat 1 or more times matching as many times as possible. |
| ?? | Repeat 0 times or 1 time matching 0 times if possible. |
| *? | Repeat 0 or more times matching as few times as possible. |
| +? | Repeat 1 or more times matching as few times as possible. |
| {n} | Repeat exactly n times. |
| {n,} | Repeat at least n times, matching as many times as possible. |
| {n,}? | Repeat at least n times, matching as few times as possible. |
| {n,m} | Repeat at least n times, but no more than m times. |
| {n,m}? | Repeat at least n times, but no more than m times while matching as few as possible. |
The following expressions allow grouped matching.
| Pattern | Description |
|---|---|
| (pattern) | Captures the specified pattern as a group. Each group is numbered automatically starting from 1. Group 0 is actually not a group at all but refers to the text matched by the entire regular expression. |
| (? |
Captures the specified pattern into the specified group name. The string used for the name must not contain any punctuation and cannot begin with a number. |
| (? |
Defines a balancing group definition. |
| (?:pattern) | Does not capture the substring matched by this pattern. Known as a noncapturing group. |
| (?imnsx-imnsx:pattern) | Applies or disables the specified options within subexpression. For more information, see .NET's Regular Expression Options. |
| (?>pattern) | Nonbacktracking (or "greedy") subexpression. |
A backreference allows a previously matched subexpression to be identified subsequently in the same regular expression.
| Pattern | Description |
|---|---|
| \number | Backreference. Matches the value of a numbered subexpression. |
| \k |
Named backreference. Matches the value of a named expression. |
Substititions are allowed only within replacement patterns.
| Pattern | Description |
|---|---|
| $number | Substitutes the last substring matched by the specified group number. The numbering scheme for groups starts at 1 (0 represents the entire match). |
| ${name} | Substitutes the last substring matched by a named group. |
| $& | Substitutes a copy of the entire match itself. |
| $` | 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 captured. |
| $_ | Substitutes the entire input string. |
The following expressions allow either/or matching.
| Pattern | Description |
|---|---|
| | | Acts as a logical OR. When between two characters or groups, matches one or the other. |
| (?(pattern)yes|no) | Matches the first pattern in the OR statement (yes) if the specified pattern is found at this point. Otherwise, matches the second pattern in the OR statement (no). |
| (?( |
Matches the first pattern in the OR statement (yes) if the specified named group is found at this point. Otherwise, matches the second pattern in the OR statement (no). |