TodayTools

Regular expressions flashcards

Patterns that find, check and replace text.

Donate
Beginner friendly, starts from zero
Section
How to study
0
Regular expressionsStart from zero · 1 of 12

What is a regex, in one plain sentence?

A regex (short for regular expression) is a short that describes the shape of the text you want, such as 'three letters, a dash, then five digits'.

A regex describes a shape, not a word.

All topics

Regular expressions at a glance

A regex is a short pattern that finds, checks or replaces text by its shape.

Like a cookie cutter you slide along the text: every piece that fits the shape is a match.

Words you will meet

Pattern
A short description of the shape of text you want
Match
A piece of text that fits the pattern
Character
One single letter, digit, space or symbol
Metacharacter
A symbol with a special job in a pattern, like a dot
Escape
A backslash that turns a special symbol back into a plain one
Quantifier
A symbol that says how many times, like + or {5}
Anchor
A symbol that pins the pattern to the start or end
Flag
A letter after the pattern that changes how it searches

The steps

  1. 1. Start from zero (12 cards): What a regex is, where you meet it, and the words you need before Core basics.
  2. 2. Core basics (14 cards): Literals, the dot, anchors, classes, quantifiers, alternation and escaping.
  3. 3. Next steps (16 cards): Groups, named captures, lazy matching, boundaries, flags, replacing and engine dialects.
  4. 4. Go deeper (16 cards): Lookarounds, backtracking and ReDoS, Unicode, safe user input, SQL, tooling and redaction.
All 12 cards in Start from zero
What is a regex, in one plain sentence?
A regex (short for regular expression) is a short pattern that describes the shape of the text you want, such as 'three letters, a dash, then five digits'.
Before regex, how did you find every phone number in a long document?
By reading it yourself, or by writing long code that checks text one character at a time. Plain search only finds exact words, not 'any ten digits'.
Where do you already meet regex in daily life?
When a form says 'enter a valid email' or 'PIN must be 6 digits', a pattern is checking you. Text editors offer it in find and replace, and spam filters use it to spot junk.
What is a good everyday picture of how a regex works?
A cookie cutter. You slide it along the text, and every piece that fits the shape exactly is a match. Change the cutter and you find a different shape.
What are a string and a character?
A string is any piece of text inside a program, usually written in quotes, like "Hello". A character is one single letter, digit, space or symbol in it.
What do match, validate, extract and replace mean?
A match is a piece of text that fits the pattern. Validate means check that the whole input fits, extract means pull the matches out, and replace means swap them for something else.
Which characters mean themselves, and which have a special job?
Most letters and digits just mean themselves. A few, called metacharacters, have jobs, like . * + ?. Putting a backslash \ in front, called escaping, turns a job back into a plain character.
What do \d and {5} mean in a pattern?
\d means any one digit from 0 to 9. {5} means repeat the thing before it exactly five times, so \d{5} is five digits in a row. A counter like {5} is called a quantifier.
What are anchors and flags?
Anchors pin a pattern to a place: ^ means the start and $ means the end. Flags are letters added after the pattern that change the search, like g for find all and i for ignore case.
Your first tiny win: what does this line find?
\d means a digit, + means one or more, and g means find all. So it pulls out every run of digits: both parts of Imran's number.
What do you need before you start?
Only a web browser. You can try every JavaScript example by opening the browser's console (press F12) and pasting it in. Nothing to install.
After this topic, what will you be able to do?
Read and write patterns that find ids, dates, phone numbers and emails, check form input properly, and clean up messy text in one replace.
Regular expressions cheatsheet

Characters and classes

  • . Any character except newline
  • \d \w \s Digit, word char, whitespace
  • \D \W \S Not digit, not word, not space
  • [aeiou] / [0-9A-F] One from a set / range
  • [^0-9] One character not in the set
  • \p{L} \p{N} \p{Script=Tamil} Unicode classes (JS u or v flag)
  • \. \( \$ \\ Literal metacharacters

Quantifiers

  • * + ? 0 or more, 1 or more, 0 or 1
  • {3} {2,5} {2,} Exact, range, at least
  • *? +? ?? Lazy versions
  • *+ ++ (?>...) Possessive / atomic (PCRE, Python 3.11+)
  • (\w+\s?)+ Nested quantifiers: ReDoS risk
  • [^"]* Fast alternative to .*?

Anchors and groups

  • ^ $ Start / end (of line with m)
  • \b \B Word boundary / not a boundary
  • \A \Z Absolute start / end (Python)
  • (...) (?:...) Capture / group only
  • (?<name>...) Named group
  • \1 \k<name> Backreference
  • a|b Alternation, lowest precedence

Lookarounds

  • (?=...) Followed by
  • (?!...) Not followed by
  • (?<=...) Preceded by
  • (?<!...) Not preceded by
  • ^(?=.*\d)(?=.*[a-z]).{12,}$ Several rules at once

JavaScript

  • /p/gimsuyv Literal with flags
  • re.test(s) true or false
  • s.match(/p/g) All matched strings
  • [...s.matchAll(/p/g)] All matches with groups and index
  • s.replace(/(\d+)/g, "[$1]") Replace using a group
  • s.replace(re, (m, g1) => ...) Computed replacement
  • RegExp.escape(userText) Escape user input (ES2025)

Python re

  • re.fullmatch(r"\d{6}", s) Whole-string validation
  • re.search(p, s) / re.match(p, s) Anywhere / at start
  • re.findall(p, s) / re.finditer(p, s) List / iterator of matches
  • re.sub(p, r"\g<1>", s, count=1) Replace with a group
  • re.split(r"\s*[,;]\s*", s) Split on a pattern
  • re.compile(p, re.I | re.M | re.X) Flags: case, lines, verbose
  • re.escape(user_text) Escape user input
Donate