Chapter 6 of 10All chapters
Chapter 6 of 10
Escaping
Matching the special characters themselves.
The metacharacters
Roughly a dozen characters have meaning: . * + ? ^ $ | ( ) [ ] { } and the backslash itself. To match one literally, put a backslash before it.
- A dot inside square brackets is already literal, so [.] needs no escape.
- Most languages offer a function to escape a whole string for use in a pattern.
Double escaping
In most languages the pattern is first a string, so the backslash must survive the string literal too. That is why a digit class is written as \\d in source code and \d in a regex tester.