Skip to main content
Guide 5 min read

10 Regex Mistakes That Break Your Pattern (With Fixes)

You write a pattern, test it on one sample line, and it works. Then it meets real data and matches nothing, matches everything or freezes your app. Regex bugs feel random, but they follow a short list. Here are ten regex mistakes that break your pattern, and the fix for each one.

Keep a tester open as you read. Paste each example into our regex tester and watch what changes. Seeing the match highlight move teaches faster than any explanation.

1. Forgetting to escape special characters

About a dozen characters carry special meaning in most regex flavors: the dot, star, plus, question mark, caret, dollar sign, pipe, backslash and all three kinds of brackets. Forget to escape one and your pattern does something you didn’t plan.

Take the pattern file.txt. The dot matches any character, so it also matches fileXtxt. The fix is a backslash: file\.txt. The same goes for prices like \$5\.99 and for anything with parentheses.

2. Letting quantifiers run greedy

Quantifiers like plus are greedy. They grab as much text as they can, then back off only if the rest of the pattern fails. Run <b>.+</b> on <b>one</b> and <b>two</b> and you get the whole string as one match, not two.

Make the quantifier lazy with a question mark, like <b>.+?</b>. Or say exactly what you want with a negated class, like <b>[^<]+</b>. The second version also runs faster.

3. Skipping the anchors

A pattern like \d{3} matches three digits anywhere, including inside 12345. That’s fine for searching, but it’s a bug for validation.

Anchor it. ^\d{3}$ accepts exactly three digits and nothing else. The caret marks the start and the dollar sign marks the end.

4. Expecting the dot to cross line breaks

The dot matches any character except a line break. So start.+end fails when start and end sit on different lines.

Turn on the dotall flag (often written s) so the dot crosses line breaks. No dotall flag in your flavor? Use [\s\S]+ instead. It matches whitespace and non whitespace alike, which means everything.

5. Putting a pipe inside square brackets

Inside square brackets the rules change. Most special characters lose their power, and the pipe is just a pipe. So [cat|dog] doesn’t match the words cat or dog. It matches one single character from the set c, a, t, pipe, d, o, g.

Use a group for whole words: (cat|dog). And remember two class rules. A caret first in the class flips it, so [^abc] means any character except a, b or c. A dash between two characters builds a range, so put a literal dash at the very end of the class.

6. Getting alternation precedence wrong

The pipe has the lowest precedence of all. ^cat|dog$ doesn’t mean a line that is exactly cat or exactly dog. It means a line that starts with cat, or a line that ends with dog.

Wrap the choices in a group: ^(cat|dog)$. Now the anchors apply to both words.

7. Triggering catastrophic backtracking

This one hurts. Nested quantifiers such as (a+)+$ give the engine a huge number of ways to split the same text. Feed it thirty letter a characters followed by a b, and the pattern can run for minutes or hang a server. When attackers use this trick on purpose, people call it ReDoS.

Fix it by removing the nesting. Write a+$ instead. Where your flavor supports them, use possessive quantifiers or atomic groups. And never run patterns from users on a live server. JavaScript, Python and Java all use backtracking engines, so all three are exposed.

8. Forgetting the flags

You wrote a perfect pattern, and it finds one match instead of ten, or none because of capital letters. Flags cause both. The global flag (g) finds every match instead of the first. The case insensitive flag (i) ignores capitals. The multiline flag (m) makes the caret and dollar sign match at every line, not just at the edges of the whole text.

Check the flag boxes in your tool before you rewrite a pattern that was fine.

9. Misusing capturing groups

Parentheses do two jobs. They group and they capture. That causes two surprises.

First, extra capturing groups shift your group numbers, so a replacement that used group 2 now grabs the wrong text. Use (?:...) for groups you need only for structure. Second, a repeated group remembers only its last pass. Run (\d)+ on 123 and group 1 holds 3, not 123. Move the quantifier inside: (\d+).

10. Assuming every flavor works the same

Regex isn’t one language. JavaScript, Python, Java, PHP and PCRE share a core, then split. Named groups look like (?<name>...) in JavaScript and (?P<name>...) in Python. Lookbehind has limits in some engines, such as the fixed length rule in Python. Possessive quantifiers and atomic groups work in Java and PCRE but not in JavaScript. Even \d differs, because Python 3 matches digits from other scripts while JavaScript matches only 0 to 9 by default.

The fix is simple. Test in the same flavor that will run your pattern in production. A pattern that passes in a browser tester can still fail in your Python script.

A quick routine for debugging any pattern

When a pattern still misbehaves, work through this routine:

  1. Shrink the pattern to the smallest piece that fails.
  2. Test it on one good sample and one bad sample.
  3. Turn on match highlighting in your regex tester.
  4. Add pieces back one at a time until the bug returns.
  5. Check the flags and the flavor.
  6. Compare the text before and after a replacement in a text diff checker.

Matching URLs or HTML? Encoded characters hide in the text. Run a sample through a URL encoder decoder or an HTML encoder decoder first, so you see what your pattern really sees. And if you’re pulling values from JSON, format it with a JSON formatter to read the structure, then parse it properly instead of matching it. Patterns show up in schedules too, and our guide on common cron mistakes has the same kind of traps.

The short version

The ten mistakes are unescaped special characters, greedy quantifiers, missing anchors, the dot and line breaks, pipes inside brackets, alternation precedence, backtracking, flags, extra groups and flavor differences. Test every pattern on good and bad samples before it ships.

Open your last broken pattern in a tester tonight and check it against this list. You’ll likely spot the culprit in under a minute.

Free browser based tools on Black Claaw to test patterns, decode text, format data and compare results.

View all tools

Advertisement