Home › Learn › Debugging lab
Learn by debugging Regex debugging lab: fix 8 real-world mistakes
Learn to identify false positives and false negatives before shipping a regular expression. Each mistake includes a broken pattern, a tested fix and two reproducible examples.
How to use this lab
Read the requirement and predict which input breaks the old pattern.
Run the valid example, then the counterexample, in Playground.
Open the explanation, compare both versions and adapt the Regex to your real requirement.
Patterns use JavaScript regular expression syntax. Several examples validate shape, not real-world data validity.
8 mistakes and fixes 4 real applications Review checklist
8 small mistakes that change your result Tests show the difference between matching the entire string and finding only a substring.
Real Regex mistake A dot is not a literal period
In a filename, the period is part of the format. The . operator matches almost any character; \. matches a period.
Broken pattern ^relatorio.csv$
Corrected pattern ^relatorio\.csv$
Should match: relatorio.csv
Should not match: relatorioXcsv
Flags: none
Why does the first pattern fail? The broken pattern also accepts relatorioXcsv, which does not follow the filename format.
Escape punctuation when your requirement calls for a literal character.
Real Regex mistake Validate the whole value, not a substring
Searching a paragraph for a postal code is different from validating a form field.
Broken pattern \d{5}-\d{3}
Corrected pattern ^\d{5}-\d{3}$
Should match: 40100-010
Should not match: CEP 40100-010!
Flags: none
Why does the first pattern fail? Without ^ and $, CEP 40100-010! contains a substring that matches.
Anchors validate a shape; they do not confirm the postal code exists.
Real Regex mistake Alternation needs grouping
The expression ^cat|dog$ means “starts with cat OR ends with dog”, not exactly one of two options.
Corrected pattern ^(?:cat|dog)$
Should match: dog
Should not match: catfish
Flags: none
Why does the first pattern fail? catfish matches the left branch. The noncapturing group puts both alternatives under the same anchors.
Group alternatives before anchoring a fixed list of accepted values.
Real Regex mistake Greedy matching crosses boundaries
When a requirement allows a single quoted field on one line, .* can absorb more than one quoted segment.
Corrected pattern ^"[^"\n]*"$
Should match: "cat"
Should not match: "cat" and "dog"
Flags: none
Why does the first pattern fail? The broken expression also accepts "cat" and "dog". A negated character class prevents extra quotes.
Use a real CSV parser for escaped quotes, multiple fields and multiline records.
Real Regex mistake Star can silently accept an empty field
For a product code that requires at least one letter, * is too permissive because it means zero or more.
Broken pattern ^SKU-[A-Z]*$
Corrected pattern ^SKU-[A-Z]+$
Should match: SKU-AB
Should not match: SKU-
Flags: none
Why does the first pattern fail? The broken pattern accepts SKU-. The + quantifier requires at least one letter.
Choose actual minimum and maximum lengths from your inventory rules.
Real Regex mistake A log level needs a boundary
Filtering with ^ERROR also accepts longer words that happen to begin with those letters.
Corrected pattern ^ERROR(?:\s|$)
Should match: ERROR requestId=ab-123
Should not match: ERRORFUL OK
Flags: none
Why does the first pattern fail? ERRORFUL OK matches the broken pattern. The fixed expression requires whitespace or end of input after ERROR.
For multiline logs use the m flag; prefer structured fields when logs are JSON.
Real Regex mistake A literal plus sign must be escaped
A code containing + should not accidentally turn it into a repetition operator.
Should match: a+b
Should not match: aaab
Flags: none
Why does the first pattern fail? The broken expression accepts aaab and rejects a+b. Escaping the plus means a literal character.
Escape metacharacters in user-supplied literals; do not concatenate raw input into a regular expression.
Real Regex mistake ASCII letters exclude many real names
A rule limited to A–Z rejects common names containing accented characters, such as João.
Broken pattern ^[A-Za-z]+$
Corrected pattern ^\p{L}+$
Should match: João
Should not match: Joao3
Flags: u
Why does the first pattern fail? With the u flag, \p{L} matches Unicode letters; this example does not permit spaces, hyphens or apostrophes.
Real-world names require a more inclusive input policy than one narrow pattern.
Real-world use cases: from Regex to product workflow
A regular expression is one component of a solution, not the whole solution. Explore four common frontend and backend workflows.
Email signup form Trim surrounding whitespace, check a commonly used shape, and then send a confirmation link before trusting the address.
Teaching pattern:
^[^\s@]+@[^\s@]+\.[^\s@]+$
Match: dev@example.com
Reject: dev@example
Important limitation: This teaching pattern does not prove mailbox ownership or implement every RFC-valid email address; also validate on the server.
Read the email guide for trade-offs.
Find failing requests in a log Select ERROR lines and capture a request identifier for correlation instead of depending on the entire message.
Teaching pattern:
^ERROR\s+requestId=(?<id>[a-z]{2}-\d{3})\b
Match: ERROR requestId=ab-123 Timeout
Reject: INFO requestId=ab-123 OK
Important limitation: The log format must be known. JSON logs and observability systems are preferable for structured events.
Learn how to extract log IDs .
SKU in an inventory system Check the textual structure of a SKU before looking up the product: allowed category, two uppercase letters and three digits.
Teaching pattern:
^(?:BK|EL|HM)-[A-Z]{2}-\d{3}$
Match: EL-AB-123
Reject: XX-AB-123
Important limitation: A pattern cannot prove the SKU exists or maps to a valid item.
Practice other formats .
Date in a booking form Check DD/MM/YYYY shape first, then parse day, month and year and verify calendar validity.
Teaching pattern:
^\d{2}/\d{2}/\d{4}$
Match: 15/11/2026
Reject: 15-11-2026
Important limitation: 31/02/2026 also matches this pattern. A calendar check requires additional code.
Read about date validation .
Full debugging walkthrough: why alternation accepts the wrong text The broken pattern ^cat|dog$ means “starts with cat OR ends with dog”. It accidentally accepts catfish and hotdog. A non-capturing group makes the anchors apply to both choices.
cat → accepted; dog → accepted; catfish and hotdog → rejected by the fixed pattern.
const broken = /^cat|dog$/;
const fixed = /^(?:cat|dog)$/;
console.log(broken.test("catfish")); // true: bug
console.log(broken.test("hotdog")); // true: bug
console.log(fixed.test("cat")); // true
console.log(fixed.test("dog")); // true
console.log(fixed.test("catfish")); // false
console.log(fixed.test("hotdog")); // false
The (?:...) group organizes alternatives without adding an unnecessary capture.
Checklist before using Regex in production
Write requirements and counterexamples before writing symbols.
Choose whether you need substring extraction or full-string validation.
Test Unicode, empty strings, boundaries and malformed input.
Avoid nested quantifiers on large untrusted inputs, and do not treat Regex as an authentication or security boundary.
Check matches in Playground and enforce business rules in application code.