Metacharacter
A metacharacter is a character that has a special meaning to a computer program, such as a shell interpreter or a regular expression engine.
In POSIX extended regular expressions, there are 15 metacharacters that must be escaped — preceded by a backslash — in order to drop their special meaning and be treated literally inside an expression: opening and closing square brackets ; backslash ; caret ; dollar sign ; period/full stop/dot ; vertical bar/pipe symbol ; question mark ; asterisk ; plus and minus signs ; opening and closing curly brackets/braces ; and opening and closing parentheses.
For example, to match the arithmetic expression
*3=6 with a regex, the correct regex is \\*3=6; otherwise, the parentheses, plus sign, and asterisk will have special meanings.Other examples
Some other characters may have special meaning in some environments.- In some Unix shells the semicolon is a Comparison of [programming languages #Statements|statement separator].
- In XML and HTML, the ampersand introduces an HTML entity. It also has special meaning in MS-DOS/Windows Command Prompt.
- In some Unix shells and MS-DOS/Windows Command Prompt, the less-than sign and greater-than sign are used for redirection and the backtick/grave accent is used for command substitution.
- In many programming languages, strings are delimited using quotes. In some cases, escape characters are used to avoid delimiter collision, e.g. "He said, \"Hello\"".
- In printf format strings, the percent sign is used to introduce format specifiers and must be escaped as "%%" to be interpreted literally. In SQL, the percent is used as a wildcard character.
- In SQL, the underscore is used to match any single character.
Escaping
The usual way to escape a character in a regex and elsewhere is by prefixing it with a backslash. Other environments may employ different methods, like MS-DOS/Windows Command Prompt, where a caret is used instead.