Quantifiers and Repetition Patterns
Writing \$ changes the instruction: the pattern now matches an actual dollar sign.
When a Symbol Changes Meaning
Regular expressions use some characters as commands rather than as ordinary text. The backslash is the escape character: it tells the regular-expression engine to treat a special character as a literal symbol. This distinction matters whenever the text you want to find contains punctuation such as a dollar sign or a decimal point.
Ask two questions whenever a pattern contains punctuation: Is the character outside square brackets, and do I want its special meaning or its literal appearance?
Tracing a Special Character
Outside square brackets, special characters can carry functional meanings in a regular expression. Inside square brackets, characters are treated literally, so they represent symbols that belong to the character set. For example, the period in [0-9.] represents a literal period alongside the digits from 0 through 9.
The escape character is the backslash. Placing it before a special character converts that character from a regex operation into a literal symbol.
Escaping Outside Brackets
The dollar sign illustrates why escaping is necessary. Outside square brackets, an unescaped dollar sign is interpreted as end of line rather than as the dollar symbol itself. Writing \$ changes the instruction: the pattern now matches an actual dollar sign.
| Pattern form | Role of the character | Use it when |
|---|---|---|
| $ | Functional regex meaning: end of line | You want that regex operation |
| \$ | Literal dollar sign | You want to match the $ symbol in text |
| [0-9.] | Character class containing digits and a literal period | You want one character from the listed set |
The backslash is needed when a special character outside brackets must be matched literally.
Building a Currency Pattern
Finding a Dollar Amount
Find the dollar amount in the text: We just received $10.00 for cookies.
- 1Match the currency symbol
Use \$ because the dollar sign is outside square brackets and must be matched as a literal dollar symbol. Without the backslash, $ is interpreted as end of line.
- 2Match the amount characters
Use [0-9.]+. The character class contains digits from 0 through 9 and a period. The plus quantifier matches one or more characters from that set.
- 3Combine the parts
The complete pattern is \$[0-9.]+. It first looks for a literal dollar sign, then one or more digits or periods.
The pattern is \$[0-9.]+, and it matches $10.00 in the supplied sentence.
The plus sign in [0-9.]+ is a quantifier. It applies to the character class immediately before it and allows one or more characters from that class. In the source pattern, the matched portion after the dollar sign can therefore contain digits and periods, with the plus sign requiring at least one such character.
The pattern has two different uses of special characters: the backslash makes $ literal, while the plus sign remains functional and controls repetition.
Mistakes with Escapes and Classes
Common mistake Writing an unescaped dollar sign when looking for a currency symbol.
What it looks like$[0-9.]+Why it failsOutside square brackets, $ is interpreted as end of line instead of as the literal dollar sign in the input.
Do this insteadUse \$[0-9.]+ when the pattern must match a dollar symbol followed by the amount.
Common mistake Assuming that every punctuation character needs a backslash inside a character class.
What it looks like[0-9\.]Why it failsCharacters inside square brackets are already treated literally, so the period in [0-9.] does not need escaping for this use.
Do this insteadUse [0-9.] for the source pattern's set of digits and a period.
Common mistake Forgetting that the plus sign controls repetition.
What it looks like\$[0-9.]Why it failsWithout +, the pattern does not express the source pattern's rule of matching one or more characters from the class.
Do this insteadUse \$[0-9.]+ when the amount portion should contain one or more digits or periods.
Common mistake Using a special character's functional meaning when the input contains that character literally.
What it looks likeTreating a punctuation mark as a regex command even though the goal is to find the punctuation in text.
Why it failsThe engine follows the character's regex meaning rather than searching for the visible symbol.
Do this insteadPrefix the special character with a backslash when it is outside square brackets and must be matched literally.
Applying the Rule
A text string contains the amount $25.50. Explain which part of the pattern matches the dollar sign, which part matches the amount characters, and why the plus sign is retained as a regex operation.
Hints
- The dollar sign is outside square brackets and must be literal.
- The character class contains digits and a period.
- The plus sign means one or more characters from the preceding character class.
Read a pattern in parts. First identify escaped symbols such as \$. Next inspect square brackets to see which characters are listed literally. Finally identify quantifiers such as + and determine which preceding character or character class they repeat.
Key Takeaways
- 1The backslash converts a special regex character into a literal symbol.
- 2A special character outside square brackets needs escaping when its visible symbol should be matched.
- 3Characters inside square brackets are treated literally, as the period is in [0-9.].
- 4The plus quantifier matches one or more characters from the preceding character class.
- 5The currency pattern \$[0-9.]+ combines a literal dollar sign with a repeated set of digits or periods.
At a glance
- Use the backslash to make special regex characters literal.
- Remember that square brackets change the way characters are interpreted: characters inside them are treated literally.
- Use a quantifier such as + to express one or more repetitions of the preceding character class.
- For the source currency example, \$[0-9.]+ matches a literal dollar sign followed by one or more digits or periods.