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.
Escaped character |
Description |
\a |
Matches a bell character, \u0007. |
\b |
In a character class, matches a backspace, \u0008. |
\t |
Matches a tab, \u0009. |
\r |
Matches a carriage return, \u000D. (\r is not equivalent to the newline character, \n.) |
\v |
Matches a vertical tab, \u000B. |
\f |
Matches a form feed, \u000C. |
\n |
Matches a new line, \u000A. |
\e |
Matches an escape, \u001B. |
\ nnn |
Uses octal representation to specify a character (nnn consists of two or three digits). |
\x nn |
Uses hexadecimal representation to specify a character (nn consists of exactly two digits). |
\c X \c x |
Matches the ASCII control character that is specified by X or x, where X or x is the letter of the control character. |
\u nnnn |
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, \* is the same as\x2A, and \. is the same as \x2E. This allows the regular expression engine to disambiguate language elements (such as * or ?) and character literals (represented by \* or\?). |
Table 1: Escaped characters
Pattern |
Matches |
\a |
"\u0007" in "Error!" + '\u0007' |
[\b]{3,} |
"\b\b\b\b" in "\b\b\b\b" |
(\w+)\t |
"item1\t", "item2\t" in "item1\titem2\t" |
\r\n(\w+) |
"\r\nThese" in "\r\nThese are\ntwo lines." |
[\v]{2,} |
"\v\v\v" in "\v\v\v" |
[\f]{2,} |
"\f\f\f" in "\f\f\f" |
\r\n(\w+) |
"\r\nThese" in "\r\nThese are\ntwo lines." |
\e |
"\x001B" in "\x001B" |
\w\040\w |
"a b", "c d" in "a bc d" |
\w\x20\w |
"a b", "c d" in "a bc d" |
\cC |
"\x0003" in "\x0003" (Ctrl-C) |
\w\u0020\w |
"a b", "c d" in "a bc d" |
\d+[\+-x\*]\d+\d+[\+-x\*\d+ |
"2+2" and "3*9" in "(2+2) * 3*9" |
Table 2: Examples for escaped characters