Regex Literal and Special Characters
This section describes the regular expression syntax for referring to literal
characters, special characters, nonprintable characters (such as a tab or a newline),
and special character escaping.
Literal Characters
The most basic form of pattern matching is the match of
literal characters. If the regular expression is
foo
and the input
string is foo
, the match will succeed because the strings are
identical. Special Characters
Certain characters are reserved for special use in
regular expressions. These special characters are called metacharacters. If you want
to use special characters as literal characters, you must escape them.
Character Name | Character | Reserved For |
|---|---|---|
opening bracket | [
| Start of a character class |
closing bracket | ]
| End of a character class |
hyphen | -
| Character ranges within a character class |
backslash | \
| General escape character |
caret | ^
| Beginning of string, negating of a character class |
dollar sign | $
| End of string |
period | .
| Matching any single character |
pipe | |
| Alternation (OR) operator |
question mark | ?
| Optional quantifier, quantifier minimizer |
asterisk | *
| Zero or more quantifier |
plus sign | +
| Once or more quantifier |
opening parenthesis | (
| Start of a subexpression group |
closing parenthesis | )
| End of a subexpression group |
opening brace | {
| Start of min/max quantifier |
closing brace | }
| End of min/max quantifier |
Escaping Special Characters
You can use these methods to treat a special character as a literal (ordinary) character:
- Precede the special character with a\(backslash character). Example: to specify an asterisk as a literal character instead of a quantifier, use\*.
- Enclose the special characters within\Q(starting quote) and\E(ending quote). Everything between\Qand\Eis then treated as literal characters.
- To escape literal double-quotes in aREGEX()expression, double the double-quotes (""). Example: to extract the inches portion from aheightfield where example values are6'2",5'11":REGEX([height], "\'(\d)+""$")
NonPrinting Characters
You can use special character sequence constructs to specify nonprintable characters in
a regular expression. Some of the most commonly used constructs are:
Construct | Matches |
|---|---|
\n
| newline character |
\r
| carriage return character |
\t
| tab character |
\f
| form feed character |