Core Concepts

regex · regular expression

/ REJ-eks /

A regex is a tiny pattern language for describing what text you're looking for — then finding, matching, or replacing it in bulk. Instead of searching for one exact word, you describe its shape: "a digit, then more digits" or "anything that looks like an email".

It's wonderfully compact. The pattern \d+ means "one or more digits"; \w+ means "a word"; ^ anchors to the start of a line and $ to the end. A few symbols can replace a whole afternoon of manual find-and-replace.

It's also famously cryptic — a dense regex can look like a cat walked across the keyboard. The trick is to build it up a piece at a time and test as you go, rather than trying to read a long one all at once.

\d{3}-\d{4}   # matches a phone like 555-1234
\w+@\w+\.\w+  # roughly matches an email address

Two small patterns: \d is a digit, \w is a word character, {3} means "exactly three".

Flavors differ slightly between languages and tools — when in doubt, test it live in a regex playground.

Also called
regexpregular expressionpattern matchinggrep