Character Classes and Square Brackets
The backslash (\) is the escape character that converts special regex characters into literal symbols
Why Literal Symbols Matter
Regular expressions use some characters as functional commands rather than as ordinary text. When the text you want to find contains one of those special characters, the backslash is the escape character: it tells the regular-expression engine to treat the following character as a literal symbol.
The central decision is whether a special character should keep its regular-expression meaning or be matched as the actual character visible in the text.
Tracing a Currency Match
Suppose the text is: We just received $10.00 for cookies. The goal is to find the dollar amount. The pattern needs to recognize a literal dollar sign, followed by characters representing the amount.
In this pattern, \$ matches the actual dollar sign. The backslash is essential because an unescaped dollar sign is interpreted as the end-of-line function rather than as the currency symbol. The [0-9.]+ portion matches one or more characters that are either digits from 0 through 9 or a period.
Character Sets in Brackets
Square brackets define a character class: a group of characters that can be matched as the allowed character set. In [0-9.]+, the class contains the digits 0 through 9 and the period character. The plus sign indicates that one or more characters from that set are matched in this pattern.
Inside and Outside Brackets
The same symbol can be treated differently depending on its location. Outside square brackets, special characters retain their regular-expression functions unless a backslash escapes them. Inside square brackets, characters are already treated literally and generally do not require escaping.
| Location | How special characters are treated | What to do when matching the symbol literally |
|---|---|---|
| Outside square brackets | They can retain their functional regular-expression meanings | Prefix the special character with a backslash |
| Inside square brackets | Characters are already treated literally | Escaping is generally not required |
Use the character's location to decide whether an escape is needed.
The Escape Transformation
Escaping changes the instruction given to the regular-expression engine. A special character outside square brackets can act as a functional command. Prefixing it with a backslash converts it into a literal match. The same principle applies to common special characters such as $, ., ^, +, *, ?, {, }, [, ], the backslash itself, and |.
Escape a special character only when it appears outside square brackets and you want the actual symbol to be matched. Do not add escapes automatically to every symbol; first identify whether the symbol is inside a character class.
Mistakes with Escaping
Leaving the dollar sign unescaped when looking for a currency amount.
Outside square brackets, the dollar sign has a regular-expression function meaning the end of a line, so it does not represent the literal currency symbol.
Fix:
Use \$ for the literal dollar sign.Assuming every character inside square brackets needs a backslash.
Characters inside square brackets are already treated literally and generally do not require escaping.
Fix:
Check the character's location before deciding whether to escape it.Reading the contents of a character class as one complete string.
The bracketed portion describes characters that may be matched; in the currency pattern, [0-9.]+ matches one or more digits or periods.
Fix:
Interpret the brackets as a character set and then interpret the following plus sign as allowing one or more matches.
Practice the Decision
A text pattern must find the literal dollar sign in a financial string. Decide whether the dollar sign should appear escaped when it is outside square brackets. Then explain why the period in the bracketed portion of [0-9.]+ does not need a separate escape for this use.
Hints
- Ask whether each symbol is outside or inside square brackets.
- Remember that the dollar sign has a functional meaning outside brackets.
- Remember that characters inside a character class are already treated literally.
What do you think happens?
For the currency text We just received $10.00 for cookies., which part of the pattern is responsible for matching the literal dollar sign?
Reveal answer
Answer: \$
The backslash changes the dollar sign from its functional regular-expression meaning into a literal dollar-sign match. The bracketed portion handles the digits and period.
Key Takeaways
- The backslash is the escape character used to make a special regular-expression character literal.
- Outside square brackets, special characters may keep their functional meanings and need escaping when the literal symbol is required.
- Inside square brackets, characters are already treated literally and generally do not need escaping.
- The currency pattern uses \$ for the literal dollar sign and [0-9.]+ for one or more digits or periods.
- Always decide how to write a symbol by checking both its special meaning and its location in the pattern.
Key Takeaways
- Use the backslash to convert a special regular-expression character into a literal match.
- Outside square brackets, escape special characters when their actual symbols must be found.
- Inside square brackets, characters are already treated literally and generally do not need escaping.
- In a currency pattern, \$ matches the dollar sign and [0-9.]+ matches one or more digits or periods.