regex

term

programming

Stands for: regular expression

A compact pattern language for matching and extracting text.

A regular expression describes a set of strings, used to validate, search, or split text. It is powerful and terse to the point of being famously write-only; poorly written patterns can also be slow enough to be a denial-of-service risk (see ReDoS).

Regular expressions are a small language for describing patterns, and their reputation for being unreadable comes mostly from being written once and read many times. The discipline that helps is the same as for code: prefer clarity over cleverness, use extended mode with whitespace and comments when the engine supports it, and name what you capture.

The dialect problem is real and underrated. POSIX, Perl-compatible, JavaScript, and Go's RE2 differ in lookahead support, backreferences, Unicode handling, and even whether a pattern that works in one engine will compile in another. A regex copied from a search result into a different runtime is a common source of subtly wrong matching rather than an outright error.

The security dimension is catastrophic backtracking. Backtracking engines can take exponential time on certain patterns, so nested quantifiers over overlapping alternatives against a crafted input become a denial of service using nothing but a text field. Some engines, RE2 among them, guarantee linear time by refusing the features that cause it. If a pattern comes from user input, or runs against untrusted input on a server, that guarantee is worth more than the expressiveness it costs.

Also known as: regexp, regular expression, regexes

All glossary entries