← Blog
// APPSEC · REGEX

Understand a regex (and avoid taking your server down with ReDoS)

Published Aug 18, 2026 · 7 min read
AppSecRegexReDoS

Regular expressions are one of those things you write once, copy a thousand times and never look at again. The problem: a regex that works can also be unreadable and, worse, a denial-of-service vector. Let's tackle both: reading them and not shooting yourself in the foot.

A regex is read token by token

Don't try to read it at a glance; break it down. Each piece is one of these:

Example: ^(?<user>[a-z0-9._%+-]+)@([a-z0-9.-]+)\.[a-z]{2,}$ reads as "start, a named group user of one or more letters/digits/symbols, an at sign, a domain, a dot and a TLD of two or more letters, end". Read that way, it stops being scary.

🧩 Paste it and I'll break it down: the Regex explainer decomposes any expression token by token in plain language, tests it live highlighting matches, and warns you if it's vulnerable to ReDoS. All in your browser.

The trap: catastrophic backtracking (ReDoS)

When something can match in many ways, the regex engine tries combinations and backtracks when it fails. With certain patterns, the number of combinations grows exponentially with input length. Result: a 30-character string can peg the CPU for seconds or minutes. That's a ReDoS (Regular expression Denial of Service).

The classic signature is a nested quantifier over something that overlaps:

(a+)+$        # group with + , quantified with + again
(.*)*         # same with .*
(\d+)*        # and with \d+
(a|a)*        # overlapping alternative, repeated

With the right input (many as followed by a character that breaks the match), the engine explores every possible partition. A famous real case was an "improved" email-validation regex that took down entire services.

How to avoid it

⚠️ ReDoS isn't theoretical: if your backend validates user input with a vulnerable regex, anyone can send the string that triggers the backtracking and burn your CPU. It's a DoS in a single request.

Encoding isn't encryption (and a regex isn't a semantic validator)

Two warnings that go together: a regex validates shape, not intent. Matching an email's format doesn't guarantee it exists, nor that a "clean" input is safe for SQL or HTML (for that, parameterized queries and escaping). The regex is a first sieve, not the defense.

Checklist

A good regex is one you understand at a glance and that can't take you down. If it fails either test, rewrite it.

Share: LinkedIn X
Sergio Belmonte Morales
Sergio Belmonte Morales
Cybersecurity Analyst · SOC · Sentinel/KQL specialist