Level 3: Advanced · Lesson 29 of 30
Debugging a Pattern
When a pattern misbehaves, shrink it, test it piece by piece, and check the usual suspects: unescaped characters, greedy wildcards, missing anchors and flags.
Every regex eventually does something you did not expect. The good news is that regex bugs fall into a small number of families, and a calm, step-by-step approach finds them quickly. Guessing and re-typing the whole pattern usually makes things worse.
Start by reproducing the problem with the smallest possible text. Paste one string that should match but does not, or one that matches but should not. A short sample makes the highlighted result easy to read and removes noise from other lines.
Next, shrink the pattern. Delete tokens from the end until the pattern matches something, then add them back one at a time. The token that makes the match disappear, or makes an unwanted match appear, is your suspect. RegexLab's explanation panel describes each token in plain English, which helps confirm that the token means what you think it means.
Check for unescaped special characters first. A dot means 'any character', so \w+.\w+ matches filetxt as happily as file.txt. Write \. for a literal dot. The same applies to $, (, +, ?, [ and |. In a string literal in your source code, remember that the backslash itself may need doubling.
Then look at quantifiers. A greedy .* or .+ that runs past the point you wanted is the second most common bug. If two values merge into one long match, switch to a negated class like [^\]]* or a lazy quantifier.
Anchors and flags come next. If a pattern with ^ and $ matches nothing in multi-line text, you probably need the m flag. If a match fails only because of letter case, you need i or a wider class. If \p{L} acts strangely in JavaScript, the u flag is missing. Also check that the language you are targeting supports every feature you used.
Finally, write down the failing sample as a test case once it is fixed. Keeping a small list of positive and negative strings beside each pattern stops the same bug from coming back later.
Live tester
1 match (JavaScript engine)
Call 555-0100 or 5550100 or 555-01000
Examples
Numbers
/\d+\.\d+/Version 3.14 but not 3x14
Matches: “3.14”
Escaping the dot fixes a pattern that would otherwise also match `3x14`.
Text
/\bcat\b/iCat, category and CAT.
Matches: “Cat”, “CAT”
Word boundaries skip `category`, and the `i` flag fixes the case mismatch.
Codes
/^[A-Z]{3}-\d{2}$/mABC-12 ABC-123 XYZ-99
Matches: “ABC-12”, “XYZ-99”
Without the `m` flag this pattern matches nothing, because the text has several lines.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Fix the pattern `\w+.\w+` so it matches only names with a real dot.
file.txt filetxt data.csv
Task 2: Fix the greedy pattern `\[.*\]` so that each bracketed item matches separately.
[a] and [b]
Task 3: Fix `^id:\d+$` so that it matches each `id:` line in multi-line text, but not lines with a prefix.
id:1 id:22 xid:3