Regular expression
Also known as: regex, regexp, regular expressions, regex pattern
A regular expression, often called regex, is a compact pattern that describes text to search for, match, or replace.
For example, a regex can find every email address, postal code, or date in a file, or check that a form field contains a valid phone number. Regexes are built into most programming languages, text editors, and spreadsheet tools. While regular expressions are incredibly powerful, they're notoriously hard to read, and badly written ones can be slow enough to crash systems.
Regular expression, explained at three levels
At home
A regular expression is like a super-powered 'find' that searches for patterns instead of exact words, such as 'any word ending in -ing.'
At work
Regexes help clean data, validate form entries, and search logs quickly.
At home
In many editors, a pattern like \d{3}-\d{4} finds phone-number-style text such as 555-1234. Google Sheets supports regex functions like REGEXMATCH.
At work
Regex syntax varies slightly between languages and tools. Testing sites let you try patterns against sample text before using them in code.
At home
AI chatbots are good at writing regexes from plain-English descriptions. In my opinion, always test them on real examples, because subtle mistakes are common.
At work
Avoid patterns vulnerable to catastrophic backtracking, set timeouts on regex processing of user input, use linear-time regex engines where available, and don't rely on regex alone to validate complex formats such as HTML.
Real-world examples
At home
Nadia had a document full of messy phone numbers. A regex in her text editor reformatted all of them consistently in one step.
At work
Marcus analyzes logs at a Winnipeg web hosting company. He uses regexes to find suspicious login patterns across millions of log lines.
How it compares
| feature | Regular expression | Simple search |
|---|---|---|
| Matches | Patterns | Exact text |
| Flexibility | Very high | Low |
| Learning curve | Steep | None |
| Example | Any 6-character Canadian postal code | The word 'Ottawa' |
Simple search finds words. Regex finds patterns.
For analysts
When specifying data validation, I'd describe the rules in plain language with examples, then let developers write the regex. It keeps requirements readable and testable.
History & origin
Mathematician Stephen Kleene described 'regular sets' in the 1950s as part of theoretical computer science. Ken Thompson brought regular expressions into practical computing in the late 1960s with the QED and ed text editors, and the Unix tool grep, created in 1973, took its name from an ed command for searching with a regular expression. Perl, released by Larry Wall in 1987, made powerful regex features popular, and its style spread to most languages. On July 2, 2019, a single badly performing regular expression in a firewall rule caused a roughly 27-minute global outage at Cloudflare. In my view, regex is one of those skills that feels like magic once it clicks.