Skip to content
  • Official
  • Beginner
  • English
  • 12 steps · 2 quizzes

Regular Expressions Practice: Exercises with Answers

Regex practice exercises with answers: a live tester marks matches as you type, one token a step, on log lines, ticket IDs, CSV exports and dates.

  • Free
  • No account needed
  • Stop any time, pick up where you left off
Workshopy.io

WorkshopyOfficial

78 lessons · used by 2 teachers

For teachers

Teaching this? Run it live with your class.

  • Students join with a code — no accounts
  • See who’s done and who’s stuck, step by step
  • It’s your copy — change anything before class

Sign in to copy it to your workspace. It’s free.

Your path

12 steps · 2 quizzes · open any step, or read it here

  1. Regex practice exercises: what you'll practiseStart hereIn this lesson you practise regular expressions with exercises and answers, in a live tester that marks every match as you type the pattern.InteractiveHands-on
    Read this step

    In this lesson you practise regular expressions with exercises and answers, in a live tester that marks every match as you type the pattern. You work on log lines, support ticket IDs, order numbers, dates in reports and emails in a CSV export, and learn one new token a step.

    A regular expression (regex) is a pattern that describes text: "TCK-, then digits" instead of every ticket number one by one. Search boxes in editors, spreadsheets, log tools, grep and every programming language understand them.

    Press Play and watch a pattern grow one token at a time. Under the pattern, each token is explained in words; in the text, every match is marked.

    Interactive · Watch a pattern grow, one token at a time — try it in the lesson

    In this lesson you'll:

    • match exact text, any character, and one character from a set
    • say how many times something repeats (+, *, ?, {4})
    • pin a match to the start or end of a line, or to a whole word
    • capture parts of a match, such as the month of a date or the domain of an email
    • check what comes before or after a match without including it

    It takes about 70 minutes. Each step has a tester with two to four tasks; a task ticks itself when your pattern finds exactly the text it wants, and the text it wants is underlined with a dashed green line. Stuck? Hint, then Show answer.

    The tester runs JavaScript's regex engine, the one in every browser. Most of what you learn works the same in Python, Java, C#, VS Code and grep -E; where they differ, the step says so.

    Takeaway: a regex is a pattern; the engine finds every place in the text that fits it.

  2. Literal text and the dot: matching exact charactersMost characters in a pattern match themselves. The pattern ERROR finds the five letters E, R, R, O, R, in that order, wherever they are. Matching is case-sensitive: error does not find ERROR. A few characters have a special meaning. The first one is the dot: .InteractiveHands-on
    Read this step

    Most characters in a pattern match themselves. The pattern ERROR finds the five letters E, R, R, O, R, in that order, wherever they are. Matching is case-sensitive: error does not find ERROR.

    A few characters have a special meaning. The first one is the dot: . matches any one character except a line break. So 09:1. matches 09:14, 09:15 and 09:1x.

    To match a real dot, escape it with a backslash: \.. The same goes for the other special characters: \$, \(, \+, \?, \*, \[, \\. Forgetting this is the classic slip: mail.example.com also matches mailXexample-com.

    Try it: do the four tasks on these log lines.

    Interactive · Literal text and the dot — try it in the lesson

    Takeaway: letters and digits match themselves; . is any one character, \. is a real dot.

  3. Character classes: one character from a setSquare brackets match one character from a set. gr[ae]y matches grey and gray; [gG] matches a small or a capital g.InteractiveHands-on
    Read this step

    Square brackets match one character from a set. gr[ae]y matches grey and gray; [gG] matches a small or a capital g.

    Class Matches one
    [aeiou] vowel
    [0-9] digit, 0 to 9
    [A-C] capital A, B or C
    [A-Za-z] letter, small or capital
    [^13] character that is not 1 or 3

    A ^ right after the opening bracket turns the set around: [^13] is anything but 1 and 3, a letter or a space included. Inside brackets most special characters lose their meaning: [.] is a real dot.

    The i flag (ignore case) makes the whole pattern ignore capitals: with it, gray also finds Gray. In JavaScript it is written after the pattern, /gray/i; in Python, re.IGNORECASE; in grep, -i.

    Try it: work on this support queue. The switch beside the pattern turns the i flag on.

    Interactive · Character classes in square brackets — try it in the lesson

    Takeaway: [...] is one character from a set, [^...] one character not in it.

  4. Shorthand classes: \d, \w and \sSome classes are so common that they have a short nameInteractiveHands-on
    Read this step

    Some classes are so common that they have a short name:

    Shorthand Means Opposite
    \d a digit, [0-9] \D, not a digit
    \w a word character, [A-Za-z0-9_] \W, not a word character
    \s whitespace: a space, a tab, a line break \S, not whitespace

    They work inside brackets too: [\d.] is a digit or a dot, [^\w\s] is punctuation.

    Where flavours differ: in JavaScript, \d is only 0 to 9 and \w is only the English letters, digits and _, even with the u flag. In Python 3, both also match other scripts: \d matches the Arabic-Indic digit ٣ and \w matches é. Plain grep -E does not know \d at all: write [0-9] there.

    Try it: find the order numbers and prices in this export.

    Interactive · Shorthand classes: \d, \w and \s — try it in the lesson

    Takeaway: \d digit, \w word character, \s whitespace; capital letters mean "not".

  5. Quantifiers: how many timesA quantifier after a character, a class or a group says how many times it repeatsInteractiveHands-on
    Read this step

    A quantifier after a character, a class or a group says how many times it repeats:

    Quantifier Means Example Matches
    + one or more TCK-\d+ TCK-7, TCK-88213
    * zero or more TCK-\d* also TCK- alone
    ? optional, zero or one colou?r colour, color
    {4} exactly 4 \d{4} 1042
    {2,5} 2 to 5 \d{2,5} 20, 88213
    {3,} 3 or more \d{3,} 771, 48213

    A quantifier repeats only what is just before it: in colou?r the ? makes only the u optional. Quantifiers are greedy: \d+ takes as many digits as it can. And matches never overlap: in 88213, \d{4} finds 8821 once and the 3 is left over.

    Try it: match ticket IDs of any length, both spellings, and runs of four digits.

    Interactive · Quantifiers: how many times — try it in the lesson

    Takeaway: + one or more, * zero or more, ? optional, {n} exactly n.

  6. Check: characters, classes and quantifiersFour questions on the steps so far.Quiz · 4 questions
    Read the questions

    Four questions on the steps so far.

    Question 1. What does the dot match in the pattern `09:1.`?

    • Any one character except a line break
    • Only a real dot
    • Any number of characters
    • Nothing: it ends the pattern

    Question 2. What does `[^0-9]` match?

    • One character that is not a digit
    • A digit at the start of the line
    • The text "0-9"
    • Every digit except 0 and 9

    Question 3. In JavaScript, what does `\d` match?

    • One digit from 0 to 9
    • A digit in any script, such as ٣
    • A whole number such as 48213
    • A decimal point

    Question 4. Which strings does `colou?r` match?

    • colour and color
    • Only colour
    • colouur and color
    • Only color
  7. Anchors and word boundaries: where the match isAnchors match a position, not a characterInteractiveHands-on
    Read this step

    Anchors match a position, not a character:

    Anchor Position
    ^ the start of the text, or of each line with the m flag
    $ the end of the text, or of each line with the m flag
    \b a word boundary: between a word character and anything else
    \B not a word boundary

    So ^ERROR finds ERROR only where a line starts with it, and \bcat\b finds the word cat but not category, catalog or bobcat.

    The m flag (multiline) changes ^ and $: without it they mean the start and end of the whole text, so ^ERROR finds at most one match. In a tool that reads a file line by line, such as grep, every line is its own text, so ^ already means "start of the line".

    A pattern for a whole value, such as a form field, is usually wrapped in both: ^ORD-\d{5}$ accepts ORD-48213 and refuses ORD-48213x and xORD-48213.

    Try it: on these log lines, turn the m flag on when a task needs it.

    Interactive · Anchors and word boundaries — try it in the lesson

    Takeaway: ^ and $ pin the start and end (per line with m); \b pins a whole word.

  8. Alternation and groups: this or thatA vertical bar means or: ERROR|WARN matches ERROR or WARN. The bar splits the whole pattern unless it sits inside brackets. 09:0\d ERROR|WARN means "09:0, a digit, a space and ERROR" or "WARN". To choose only the level, group it: 09:0\d (ERROR|WARN).InteractiveHands-on
    Read this step

    A vertical bar means or: ERROR|WARN matches ERROR or WARN.

    The bar splits the whole pattern unless it sits inside brackets. 09:0\d ERROR|WARN means "09:0, a digit, a space and ERROR" or "WARN". To choose only the level, group it: 09:0\d (ERROR|WARN).

    Brackets also let a quantifier repeat more than one character: (\.\d+)+ is one or more groups of "a dot and digits", which is how a version number like 1.12.4 is built.

    A group you don't need to read back can be written (?:...): it groups without capturing. The next step is about capturing.

    Try it: find the ERROR and WARN lines and the version numbers. The table under the text shows what each group captured.

    Interactive · Alternation and groups — try it in the lesson

    Takeaway: | is or; brackets limit the or and let a quantifier repeat a whole group.

  9. Capture groups: taking a match apartEvery pair of brackets is a capture group: besides the whole match, the engine keeps what each group matched, numbered from the left by its opening bracket. In \d{4}-(\d{2})-\d{2}, group 1 is the month.InteractiveHands-on
    Read this step

    Every pair of brackets is a capture group: besides the whole match, the engine keeps what each group matched, numbered from the left by its opening bracket. In \d{4}-(\d{2})-\d{2}, group 1 is the month.

    That is how you pull data out of text: the year, month and day of each date; the user name and the domain of each email address.

    A group can have a name: (?<year>\d{4}). Code then reads it by name instead of by number, which survives adding another group in front. Python writes a named group (?P<year>\d{4}).

    In a find-and-replace, the groups come back: $1 (or $<year>) in JavaScript and VS Code, \1 in Python and sed. Replacing (\d{4})-(\d{2})-(\d{2}) with $3.$2.$1 turns 2026-11-30 into 30.11.2026.

    Try it: match the dates in these report lines, then capture their parts.

    Interactive · Capture groups and named groups — try it in the lesson

    Takeaway: brackets capture; groups are numbered by their opening bracket, or named with (?<name>...).

  10. Greedy and lazy: how much the match takesQuantifiers are greedy: they take as much as they can and give back only what the rest of the pattern needs. On the CSV row "Dana Levi","dana@example.com","Sales", the pattern "." matches from the first quote to the last one: the whole row.InteractiveHands-on
    Read this step

    Quantifiers are greedy: they take as much as they can and give back only what the rest of the pattern needs. On the CSV row "Dana Levi","dana@example.com","Sales", the pattern ".*" matches from the first quote to the last one: the whole row.

    Two ways to stop at the first closing quote:

    • Lazy: add ? after the quantifier. ".*?" takes as little as it can: "Dana Levi", then "dana@example.com", then "Sales".
    • A negated class: "[^"]*" allows anything but a quote between the quotes. It can't run past a closing quote, and it is often faster.

    Lazy quantifiers exist in JavaScript, Python, PCRE (grep -P) and most languages, but not in POSIX grep -E. The negated class works everywhere.

    One warning: nested repeats such as (\w+\s?)+$ can make an engine try millions of ways before it gives up on a line that almost matches (catastrophic backtracking). This tester stops such a pattern after half a second; a server may not.

    Try it: the tester starts with the greedy pattern. Make each field its own match.

    Interactive · Greedy and lazy quantifiers — try it in the lesson

    Takeaway: .* takes as much as it can; .*? or [^"]* stops at the first quote.

Comments

You’ll sign in with Google before it’s posted.Comments are public — don’t name your students.
0 / 1,500

No comments yet. Be the first to say how it went.

Using it

Workshopy shares this lesson under CC BY 4.0. Anyone may use and change it, with credit to you. Anyone can learn it here, free. A copy in your workspace is yours to change and run live; your students never need an account.

Versions

  1. 1

    Version 1Current

    · First published

Copies are made from the current version. A copy you already have doesn’t change.

More from Workshopy

All 78 lessons
All 13 Programming lessons