The story behind the regex tester

A regular expression is a tiny program that describes a set of strings. The idea began as mathematics about nerve cells, became a search command in a text editor, and is now built into almost every programming language.

A pattern between two slashes above lines of text with the matching words highlighted
A pattern between two slashes above lines of text with the matching words highlighted

From nerve nets to the Kleene star

In 1951 the logician Stephen Kleene wrote a report for the RAND Corporation on what the networks of idealised neurons of McCulloch and Pitts could recognise. He described those patterns with three operations: one thing after another, a choice between two things, and any number of repetitions. He called the result regular events, and the repetition is still written with his star: a* means any number of a's. Kleene proved that exactly these patterns are the ones a machine with a fixed, finite memory can recognise.

Thompson, ed and grep

Ken Thompson turned the theory into a tool. In 1968 he published a way to compile a regular expression into a small program that tries every possible path at once, and he built it into the QED editor and later into ed on Unix. The ed command g/re/p, globally search for a regular expression and print, became a program of its own in 1973: grep. The dot, the star, the brackets and the anchors ^ and $ of a modern pattern come straight from that editor.

Perl, PCRE and JavaScript

Larry Wall's Perl, released in 1987, made regular expressions the centre of a language, and Perl 5 added lazy quantifiers, lookahead and much more in 1994. Philip Hazel's PCRE library of 1997 brought that Perl style to PHP, Apache and countless other programs, and most languages copied it. JavaScript has had regular expressions since the late 1990s; it added the sticky y and Unicode u flags in 2015, the s flag, lookbehind and named groups in 2018, match indices (d) in 2022 and the v flag for set operations in 2024. This tester uses your browser's own engine, so what works here works in your JavaScript code.

Why some patterns never finish

Engines in the Perl tradition backtrack: when a path fails they go back and try the next one. For a pattern such as (a+)+$ on a long row of a's that does not end in a match, the number of ways to split the a's between the two pluses doubles with every extra letter, so 30 letters already mean about a billion attempts. Such regular expression denial of service has taken real sites down: a pattern that trimmed whitespace stopped Stack Overflow for 34 minutes in 2016, and one in a firewall rule took Cloudflare offline in 2019. Engines that follow Thompson's method, such as Russ Cox's RE2, always finish in linear time, but give up back-references to do so. The tester runs each pattern in a separate worker and stops it after 1.5 seconds.

Frequently asked questions

What is the difference between a greedy and a lazy quantifier?

A greedy quantifier such as .* first takes as much as it can and gives characters back only when the rest of the pattern needs them. A lazy one such as .*? takes as little as possible and only grows when it has to. On <b>bold</b> the pattern <.*> matches the whole string, <.*?> only <b>.

Are regular expressions in programming really regular?

Not quite. A back-reference such as (\w+) \1 can match the same word twice, which no finite machine can check for words of any length, so modern patterns describe more than Kleene's regular languages. Matching with back-references is NP-hard in general, which is one reason why fast engines such as RE2 leave them out.

Who said that regular expressions give you two problems?

Jamie Zawinski, in a Usenet post in 1997: some people, when confronted with a problem, think I know, I'll use regular expressions; now they have two problems. It is still quoted as a warning not to parse everything, such as HTML, with a pattern.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

Open the regex tester