What a regular expression is

Beginner · 8 min

A regular expression describes the shape of text, not its spelling. Where regex shows up, how an engine reads a pattern, and a short history to grep.

Kris Jordan · 8 min

What a regular expression is

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kris JordanReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A regular expression is a pattern that describes the shape of a piece of text, so one pattern can match many different strings.
  • Plain string search needs the exact spelling. A pattern like \d{4}-\d{2}-\d{2} finds every date without knowing any of them in advance.
  • The engine scans left to right and reports the leftmost match, with its text and its index. If nothing matches, you get null or an empty result.
  • The same idea runs in grep, sed, awk, editors and every mainstream language, but each tool has its own dialect (called a flavour).
  • Regex is a text tool, not a parser. It is excellent at shapes like dates and IDs and a poor fit for nested structures such as HTML.

Searching for something you cannot spell

Take a four-line file of invoice notes. You want every date in it.

Invoice 1042 was paid on 2026-03-14 by ada@example.com.
Invoice 1043 is due 2026-04-02. Call 555-0142 if late.
Refund for invoice 1031 issued 2026-02-27 (see grace@example.org).
No dates in this line, only the number 2026 and a version 3.14.

Ordinary search, the kind behind Ctrl-F, answers one question: does this exact string appear? "2026-03-14".includes("2026-03-15") is false, because one character differs. To find all the dates you would have to already know all the dates, and then you wouldn’t need to search.

What you do know is what a date looks like: four digits, a dash, two digits, a dash, two digits. A regular expression (regex, regexp) is a way to write that down. It is a small pattern language for describing the shape of text, and a program called a regex engine tests strings against it.

The first pattern

In JavaScript, a pattern between slashes is a regex literal. \d means “any one digit” and {4} means “four of them”:

const text = require("fs").readFileSync("notes.txt", "utf8");
text.match(/\d{4}-\d{2}-\d{2}/g);
// [ '2026-03-14', '2026-04-02', '2026-02-27' ]

Three dates, and the stray 2026 and 3.14 on the last line did not match, because they don’t have the shape. The g after the closing slash means “keep going after the first match”. Without it you get only the first.

Here is the same shape in four other tools I ran on this machine. The pattern text barely changes:

$ /usr/bin/grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2}' notes.txt
2026-03-14
2026-04-02
2026-02-27
$ python3 -I -c 'import re; print(re.findall(r"\d{4}-\d{2}-\d{2}", open("notes.txt").read()))'
['2026-03-14', '2026-04-02', '2026-02-27']
$ perl -ne 'print "$1\n" while /(\d{4}-\d{2}-\d{2})/g' notes.txt
2026-03-14
2026-04-02
2026-02-27
$ sed -E 's/[0-9]{4}-[0-9]{2}-[0-9]{2}/<DATE>/' notes.txt | head -2
Invoice 1042 was paid on <DATE> by ada@example.com.
Invoice 1043 is due <DATE>. Call 555-0142 if late.

grep -E uses [0-9] where the others use \d. That is a first sign of the theme of this whole roadmap: regex is one idea with many dialects. The flavors lesson maps them out.

How the engine reads a pattern

Think of the pattern as a stencil. The engine lays it over the text at position 0 and checks whether it fits. If not, it slides the stencil one character right and tries again. The first position where it fits gives the leftmost match:

const m = "xx 2026-03-14 yy 2026-04-02".match(/\d{4}-\d{2}-\d{2}/);
console.log(m[0], m.index);   // 2026-03-14 3

A pattern is also a compact way to describe a whole set of strings. ^(cat|dog)s?$ describes exactly four of them. I tested five inputs:

"cat"    true
"dogs"   true
"bird"   false
"cats!"  false
"catdog" false

Reading a pattern means asking which strings it accepts. Always test strings it should reject as well as ones it should accept. A pattern that matches everything is easy to write.

Where regex shows up

You will meet it in grep and sed (see searching with grep and editing streams with sed), in the search box of most code editors, in input validation, in log parsing, and in URL routing in web frameworks. JavaScript, Python and Perl all ship it with the language. This roadmap uses JavaScript as its main dialect, because it runs in every browser, and runs Python, Perl and the POSIX tools beside it when they differ. The language side of regex in JavaScript is covered in regular expressions.

A short history

As Wikipedia tells it, the mathematician Stephen Kleene described “regular events” in 1951. Ken Thompson then built Kleene’s notation into the QED editor and later into the Unix editor ed. In ed, the command g/re/p meant: globally search for a regular expression and print matching lines. That command became its own program, and the program kept the name, grep. Perl’s regex features came in the 1980s and PCRE, a library that copies them, in 1997. I read those dates and could not check them against primary sources.

About the video

Kris Jordan’s “Regular Expressions: A Mysterious Introduction” was published on 13 May 2020 and runs 8 minutes 27 seconds. Kris Jordan is a computer science professor at the University of North Carolina at Chapel Hill, per his channel description. The description says the video assumes no prior knowledge, works as an interactive mystery puzzle in Visual Studio Code, and introduces the “and then” (concatenation) operator along with ^, $ and .. Its chapters are Introduction, Regular Expressions Demo, Setup and Review. I couldn’t read a transcript, so I can’t say how it phrases anything. It does not, from its description, cover the history above or the differences between flavours; both come from this page. English per its title and description.

Computerphile’s two videos in the “deeper” list cover the theory and the grep origin story. I haven’t heard them either and judge them by title and description only.

What I did not run

The history comes from reading Wikipedia, not from primary sources. I didn’t run regex in editors, HTML pattern attributes or framework routers, so those uses are claims from general knowledge. I didn’t use GNU grep or regex101.

Next

The pattern above used - and {4} without explanation. Literals and escaping starts from the simplest possible pattern, plain characters, and then shows which characters are not plain.

Check yourself

  1. 01Why can't a plain string search find every date in a file?

    It needs the exact characters, and you don't know the dates beforehand. A regex describes the shape (four digits, a dash, two digits, a dash, two digits), so it matches every string with that shape.

  2. 02What does the engine return for /\d{4}-\d{2}-\d{2}/ on 'xx 2026-03-14 yy 2026-04-02'?

    The first match only, 2026-03-14, at index 3. Without the g flag the search stops at the leftmost match.

  3. 03Does the pattern /^(cat|dog)s?$/ match 'catdog'?

    No. The anchors force the whole string to be exactly one of cat, cats, dog or dogs, and 'catdog' is none of those.

  4. 04Where does the name grep come from?

    From the old ed editor command g/re/p, meaning globally search for a regular expression and print the matching lines.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark