⌘K

Developer

Regex

A regular expression (regex) is a compact pattern language for finding, validating and replacing text that matches a described shape rather than a fixed string.

Updated 12 Aug 2026

Describing text by shape

Plain search finds text you already know. A regular expression finds text you can only describe: three digits, then a dash, then four digits; or any word at the start of a line followed by a colon. The engine walks the input and reports every span that fits the pattern.

The syntax you actually need

PatternMatches
.Any character except a newline
\d \w \sDigit, word character, whitespace
[a-f]Any one character in the range
* + ?Zero or more, one or more, optional
{2,5}Between two and five repeats
^ $Start and end of the string or line
(...)Capture group you can reference in a replacement
(?:...)Group without capturing
a|bEither alternative

Greedy versus lazy

Quantifiers are greedy by default: they take as much as possible and give characters back only when the rest of the pattern fails. Adding a question mark makes them lazy. Matching quotes with ".*" grabs everything from the first quote to the last on a line, while ".*?" stops at the first closing quote — a difference that explains a large share of confusing regex bugs.

Do not use regex to parse HTML, JSON or CSV. Nested and quoted structures need a real parser; a pattern will look right on your samples and fail in production.

Regex Tester

Test patterns against sample text with live match highlighting.

Try Regex Tester

Characteristics

  • Pattern language available in almost every programming language
  • Flags control case sensitivity, global matching and multiline behaviour
  • Capture groups extract parts of a match for reuse
  • Dialects differ — JavaScript, PCRE and POSIX are not identical

Common uses

  • Validating emails, postcodes, phone numbers and identifiers
  • Search and replace across a codebase or log file
  • Extracting fields from unstructured text
  • Routing and redirect rules in web servers

Advantages

  • Extremely concise for text matching that would take many lines of code
  • Portable knowledge across languages and editors
  • Built into editors, grep, databases and web server configuration
  • Capture groups make structured extraction straightforward

Limitations

  • Hard to read and maintain as patterns grow
  • Catastrophic backtracking can hang a process on hostile input
  • Dialect differences break patterns moved between languages
  • Unsuitable for nested or recursive formats

Examples

Common practical patterns
^\d{4}-\d{2}-\d{2}$        # ISO date
^[\w.+-]+@[\w-]+\.[\w.]{2,}$  # pragmatic email check
(\d{3})-(\d{4})              # capture two groups
\b(\w+)\s+\1\b               # repeated word

Frequently asked questions

What is regex used for?

Validating input, searching and replacing text, and extracting fields from unstructured content such as logs.

Is regex a programming language?

It is a pattern language, not a general-purpose one — it describes text shapes but has no variables or control flow.

Why is my regex matching too much?

Quantifiers are greedy by default. Add ? to make them lazy, or use a negated character class.

Can regex parse HTML?

No. HTML nests arbitrarily; use a DOM parser instead.

What is catastrophic backtracking?

A pattern with nested ambiguous quantifiers can explore exponentially many paths, freezing on certain inputs.

Related terms

All terms

Related tools

Related guides

All guides