Regular expressions flashcards
Patterns that find, check and replace text.
Beginner friendly, starts from zero
0
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. Start from zero (12 cards): What a regex is, where you meet it, and the words you need before Core basics.
- 2. Core basics (14 cards): Literals, the dot, anchors, classes, quantifiers, alternation and escaping.
- 3. Next steps (16 cards): Groups, named captures, lazy matching, boundaries, flags, replacing and engine dialects.
- 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
\dand{5}mean in a pattern? \dmeans 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, likegfor find all andifor ignore case. - Your first tiny win: what does this line find?
\dmeans a digit,+means one or more, andgmeans 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 \sDigit, word char, whitespace\D \W \SNot 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 (JSuorvflag)\. \( \$ \\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 withm)\b \BWord boundary / not a boundary\A \ZAbsolute start / end (Python)(...) (?:...)Capture / group only(?<name>...)Named group\1 \k<name>Backreferencea|bAlternation, lowest precedence
Lookarounds
(?=...)Followed by(?!...)Not followed by(?<=...)Preceded by(?<!...)Not preceded by^(?=.*\d)(?=.*[a-z]).{12,}$Several rules at once
JavaScript
/p/gimsuyvLiteral with flagsre.test(s)trueorfalses.match(/p/g)All matched strings[...s.matchAll(/p/g)]All matches with groups and indexs.replace(/(\d+)/g, "[$1]")Replace using a groups.replace(re, (m, g1) => ...)Computed replacementRegExp.escape(userText)Escape user input (ES2025)
Python re
re.fullmatch(r"\d{6}", s)Whole-string validationre.search(p, s) / re.match(p, s)Anywhere / at startre.findall(p, s) / re.finditer(p, s)List / iterator of matchesre.sub(p, r"\g<1>", s, count=1)Replace with a groupre.split(r"\s*[,;]\s*", s)Split on a patternre.compile(p, re.I | re.M | re.X)Flags: case, lines, verbosere.escape(user_text)Escape user input