Programming concepts
What is a Regular Expression?
Definition
A regular expression is a pattern written in a compact syntax that describes a set of strings, used to search, validate, extract and replace text.
Also known as: Regex, Regexp, RegExp
Regular Expression in more detail
A pattern is built from literal characters plus metacharacters that mean something more: a dot for any character, character classes in square brackets, quantifiers for how many times, anchors for the start and end of the input, groups for capturing and alternation for choice. The engine reads the pattern and reports where — and whether — the subject text matches, which is what makes one line of regex replace a page of manual string handling.
Flavours differ more than people expect. PCRE, POSIX, JavaScript, Python, Java, Go's RE2 and the various shell tools disagree about lookbehind, named groups, Unicode properties, lazy quantifiers and even which characters need escaping. Patterns copied between languages are a routine source of bugs, so test them in the engine that will actually run them.
Two failure modes are worth knowing before you write anything serious. Backtracking engines can take exponential time on certain patterns — nested quantifiers over overlapping alternatives, the classic being a pattern like (a+)+b against a long run of a characters — which turns a validation call into a denial-of-service vector known as ReDoS. And regular expressions genuinely cannot parse arbitrarily nested structures such as HTML or JSON, because those need to count nesting depth and a regular language cannot. Use a parser for structure and regexes for the flat, well-defined patterns they are good at.
That includes being sceptical of the famous ones. Email validation by regex is either wrong or unreadable — the practical approach is to check for one at sign with something on either side, and then confirm the address by sending mail to it.
Regular Expression example
\d{4}-\d{2}-\d{2} a date like 2026-03-04
^\s*$ a line that is empty or whitespace
(?<year>\d{4})-(?<mon>\d{2}) named capture groups
colou?r "color" or "colour"
\bcat\b the word cat, not "category"
[^,]+ everything up to the next comma
(?=.*\d) lookahead: contains a digit somewhere
Greedy vs lazy
<.+> on "<a><b>" matches <a><b> (greedy)
<.+?> on "<a><b>" matches <a> (lazy)
// JavaScript
"2026-03-04".match(/(\d{4})-(\d{2})-(\d{2})/).slice(1)
-> ["2026", "03", "04"]
Catastrophic backtracking — do not ship this
/^(a+)+$/.test("aaaaaaaaaaaaaaaaaaaaaaaaaaaa!") // hangsRegular Expression — common questions
- Why does my regex work in one language but not another?
- Because engines implement different flavours. Lookbehind, named groups, Unicode property escapes and escaping rules all vary between PCRE, JavaScript, Python, Java and RE2. Always test a pattern in the engine that will execute it.
- Can I parse HTML with a regular expression?
- Not reliably. HTML nests arbitrarily and regular languages cannot count nesting depth, so any pattern breaks on valid input eventually. Use an HTML parser; keep regexes for flat, predictable fragments.
- What is catastrophic backtracking?
- A pattern whose nested quantifiers force a backtracking engine to try exponentially many combinations on a non-matching input, so matching hangs. It becomes a denial-of-service risk when the input is user-supplied; linear-time engines such as RE2 avoid it by design.
Keep reading
This page explains what Regular Expression means. For the syntax itself, the regex cheat sheet is a full reference of real commands and examples. Every other definition is in the A-Z glossary.
Browse the A-Z glossary