Regular expression control characters

Control characters are metacharacters, operators, and replacement text characters that can be used in regular expressions.

Only half-width characters are recognized. Any full-width characters that correspond to the characters in the following tables are not recognized.

Table 1. Regular expression metacharacters
Character Allowed outside of sets Allowed inside of sets Description
\a Yes Yes Match a BELL, \u0007
\A Yes   Match at the beginning of the input. Differs from ^ in that \A does not match after a new line within the input.
\b Yes   Match if the current position is a word boundary. Boundaries occur at the transitions between word (\w) and non-word (\W) characters, with combining marks ignored.
\B Yes   Match if the current position is not a word boundary.
\cX Yes Yes Match a control-X character.
\d Yes Yes Match any character with the Unicode General Category of Nd (Number, Decimal Digit.)
\D Yes Yes Match any character that is not a decimal digit.
\e Yes Yes Match an ESCAPE, \u001B
\E Yes Yes Terminates a \Q ... \E quoted sequence.
\f Yes Yes Match a FORM FEED, \u000C.
\G Yes   Match if the current position is at the end of the previous match.
\n Yes Yes Match a LINE FEED, \u000A
\N{UNICODE CHARACTER NAME} Yes Yes Match the named character.
\p{UNICODE PROPERTY NAME} Yes Yes Match any character with the specified Unicode Property.
\P{UNICODE PROPERTY NAME} Yes Yes Match any character not having the specified Unicode Property.
\Q Yes Yes Places quotation marks around all following characters until \E
\r Yes Yes Match a CARRIAGE RETURN, \u000D
\s Yes Yes Match a white space character. White space is defined as [\t\n\f\r\p{Z}]
\S Yes Yes Match a non-white space character.
\t Yes Yes Match a HORIZONTAL TABULATION, \u0009
\uhhhh Yes Yes Match the character with the hex value hhhh
\Uhhhhhhhh Yes Yes Match the character with the hex value hhhhhhhh. Exactly 8 hex digits must be provided, even though the largest Unicode code point is \U0010ffff
\w Yes Yes Match a word character. Word characters are as follows:

[\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\u200c\u200d]

\W Yes Yes Match a non-word character.
\x{hhhh} Yes Yes Match the character with hex value hhhh. From one to 6 hex digits can be supplied.
\xhh Yes Yes Match the character with two-digit hex value hh
\X Yes   Match a Grapheme Cluster.
\Z Yes   Match if the current position is at the end of input, but before the final line terminator, if one exists.
\z Yes   Match if the current position is at the end of input.
\n Yes   Back Reference. Match whatever the nth capturing group matched. n must be a number > 1 and < total number of capture groups in the pattern.
\0ooo Yes Yes Match an Octal character. 'ooo' is from one to three octal digits. 0377 is the largest allowed Octal character. The leading zero is required; it distinguishes Octal constants from back references.
[pattern] Yes Yes Match any one character from the set.
. Yes   Match any character.
^ Yes   Match at the beginning of a line.
$ Yes   Match at the end of a line.
\ Yes   Places quotation marks around the character that follows. Characters that must have surrounding quotation marks to be treated as literals are * ? + [ ( ) { } ^ $ | \ . /
\   Yes Places quotation marks around the character that follows. Characters that must be quoted to be treated as literals are [ ] \
Characters that might need to be quoted, depending on the context are - &
Table 2. Regular expression operators
Operator Description
| Alternation. A|B matches either A or B
* Match 0 or more times. Match as many times as possible.
+ Match 1 or more times. Match as many times as possible.
? Match zero or one time. Prefer one.
{n} Match exactly n times.
{n,} Match at least n times. Match as many times as possible.
{n,m} Match between n and m times. Match as many times as possible, but not more than m.
*? Match 0 or more times. Match as few times as possible.
+? Match 1 or more times. Match as few times as possible.
?? Match zero or one time. Prefer zero.
{n}? Match exactly n times.
{n,}? Match at least n times, but no more than required for an overall pattern match.
{n,m}? Match between n and m times. Match as few times as possible, but not less than n
*+ Match 0 or more times. Match as many times as possible when first encountered, do not retry with fewer even if overall match fails (Possessive Match)
++ Match 1 or more times. Possessive match.
?+ Match zero or 1 time. Possessive match.
{n}+ Match exactly n times.
{n,}+ Match at least n times. Possessive Match.
{n,m}+ Match between n and m times. Possessive Match.
( ... ) Capturing parentheses. Range of input that matched the parenthesized subexpression is available after the match.
(?: ... ) Non-capturing parentheses. Groups the included pattern, but does not provide capturing of matching text. More efficient than capturing parentheses.
(?> ... ) Atomic-match parentheses. First match of the parenthesized subexpression is the only one tried. If it does not lead to an overall pattern match, back up the search for a match to a position before the "(?>"
(?# ... ) Free-format comment (?# comment )
(?= ... ) Look-ahead assertion. True if the parenthesized pattern matches at the current input position, but does not advance the input position.
(?! ... ) Negative look-ahead assertion. True if the parenthesized pattern does not match at the current input position. Does not advance the input position.
(?<= ... ) Look-behind assertion. True if the parenthesized pattern matches text that precedes the current input position. The last character of the match is the input character just before the current position. Does not alter the input position. The length of possible strings that is matched by the look-behind pattern must not be unbounded (no * or + operators.)
(?<!...) Negative Look-behind assertion. True if the parenthesized pattern does not match text that precedes preceding the current input position. The last character of the match is the input character just before the current position. Does not alter the input position. The length of possible strings that is matched by the look-behind pattern must not be unbounded (no * or + operators.)
(?ismwx-ismwx: ... ) Flag settings. Evaluate the parenthesized expression with the specified flags enabled or disabled.
(?ismx-ismx) Flag settings. Change the flag settings. Changes apply to the portion of the pattern that follows the setting. For example, (?i) changes to a not case-sensitive match.
Table 3. Set expressions (character classes)
Example expression Description
[abc] Match any of the characters a, b, or c
[^abc] Negation - match any character except a, b, or c
[A-M] Range - match any character from A to M. The characters to include are determined by Unicode code point order.
[\u0000-\U0010ffff] Range - match all characters.
[\p{Letter}] 
[\p{General_Category=Letter}] 
[\p{L}]
Characters with Unicode Category = Letter. All forms that are shown are equivalent.
[\P{Letter}] Negated property. (Uppercase \P) Match everything except Letters.
[\p{numeric_value=9}] Match all numbers with a numeric value of 9. Any Unicode Property might be used in set expressions.
[\p{Letter}&&\p{script=cyrillic}] Logical AND or intersection. Match the set of all Cyrillic letters.
[\p{Letter}--\p{script=latin}] Subtraction. Match all non-Latin letters.
[[a-z][A-Z][0-9]] [a-zA-Z0-9]] Implicit Logical OR or Union of Sets. The examples match ASCII letters and digits. The two forms are equivalent.
[:script=Greek:] Alternate POSIX-like syntax for properties. Equivalent to \p{script=Greek}