Regular Expressions: Practice Questions
100 questions. Try each one yourself before checking the answer.
Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.
Use re.search() to look for a pattern anywhere in a string, and show what it returns when it finds one and when it does not.
Use re.match() and show how it differs from re.search().
Use re.fullmatch() to require that the pattern covers the entire string.
Take the Match object apart: what text matched, and where.
Use re.findall() to get every match, not just the first.
Match text that contains regex special characters.
Use . to stand for any single character.
Use [...] to allow one character out of a chosen set.
Use [^...] to match any character except the listed ones.
Use - inside a class to describe a range of characters.
Use the digit shorthands \d and \D.
Use the word-character shorthands \w and \W.
Use the whitespace shorthands \s and \S.
Use * to mean "zero or more of the thing before it".
Use + to mean "one or more", and use it to pull whole numbers out of text.
Use ? to mark something as optional.
Use {n}, {n,} and {n,m} to give an exact count.
Use ^ and $ to pin a pattern to the start or end of the text.
Use \b to match whole words only.
Use | to accept any one of several alternatives.
Use ( ) to apply a quantifier to a whole chunk rather than one character.
Use a capturing group to pull one piece out of a larger match.
Use re.sub() to replace every match with something else.
Use re.split() to split on a pattern instead of a fixed string.
Compile a pattern once and reuse the compiled object.
Show exactly what goes wrong when a pattern is not written as a raw string.
Capture several fields at once and unpack them.
Give the groups names instead of numbers.
Use (?:...) when you want brackets for structure but not for capture.
Handle a group that may not take part in the match at all.
Show how the number of capturing groups changes what re.findall() returns.
Use re.finditer() to get Match objects, with positions, one at a time.
Reuse captured text inside the replacement string.
Pass a function as the replacement so each match can be computed.
Find out how many replacements happened, and limit them.
Keep the separators, or limit how many splits happen.
Build a pattern safely from text you did not write.
Use flags to change how the whole pattern behaves, starting with case.
Make ^ and $ work per line instead of per string.
Let . match newlines as well.
Write a long pattern that a human can still read.
Combine flags with |, and set them inside the pattern.
Show the difference between greedy and lazy quantifiers.
Refer back to a captured group inside the pattern itself.
Use (?=...) and (?!...) to check what follows without consuming it.
Use (?<=...) and (?<!...) to check what came before.
Use the anchors that ignore re.MULTILINE.
Inspect a compiled pattern and search only part of a string.
Learn which characters are special inside [ ] and which are not.
Bring the band together: a dictionary of compiled patterns used as one extractor.
Write an email validator, and be honest about what it does and does not catch.
Accept phone numbers in any common format and store them in one canonical form.
Mask sensitive values while leaving enough of each one to be recognisable.
Write a password checker that reports each rule separately instead of a bare pass/fail.
Break a URL into its parts with one named-group pattern.
Pull hashtags and mentions out of social posts and rank them.
Parse a log file with a compiled named-group pattern and finditer().
Build a word-frequency report from a block of prose.
Build a text-cleaning pipeline out of small named substitutions.
Find every kind of number in a messy line of text.
Convert between camelCase, snake_case and kebab-case.
Fill {placeholder} slots from a dictionary, reporting anything missing.
Turn any article title into a safe URL slug.
Validate a date's format with a regex, then its values with Python.
Split a comma-separated line without breaking quoted fields.
Extract links, images and code spans from a Markdown document.
Redact a text file and write an audit trail beside it.
Strip HTML down to readable text โ and see where the approach starts to fail.
Turn an arithmetic expression into a stream of typed tokens with one alternation.
Write a small grep: search a file for a pattern and report line numbers with context.
This PIN-code validator accepts values it should reject. Find the bug and fix it.
import re
def is_valid_pin(pin):
return re.match(r"\d{6}", pin) is not None
print(is_valid_pin("411045"))
print(is_valid_pin("4110451234"))
print(is_valid_pin("411045abc"))Show what happens when a pattern is able to match an empty string.
Show the two places newlines quietly change what a pattern means.
This quote extractor returns one huge string instead of three separate quotes. Explain and fix it.
import re
text = 'She said "yes" then "maybe" and finally "no".'
print(re.findall(r'".*"', text))Show that the replacement string has its own escape rules, separate from the pattern.
Show that alternation picks the first branch that works, not the longest.
Show what \w, \d and \b actually match once the text is not plain ASCII.
Measure a pattern that takes exponential time, and see how fast it gets out of hand.
Rewrite a catastrophic pattern three different ways and time each one.
Measure what re.compile() actually saves, and find the case where it matters most.
Find matches that overlap, which findall() will never give you.
Show that a quantified group keeps only its final repetition.
Show which escapes change meaning inside a character class.
Compare a regex against the plain string method for the same job.
Show something a regular expression provably cannot do.
Build a Web Access Log Analyser that turns raw server lines into a traffic report.
Build a Contact Importer that cleans messy input and reports what it could not fix.
Build a Config File Parser with sections, comments and variable interpolation.
Build a Markdown to HTML converter for the common inline and block elements.
Build a Search and Replace Tool that previews its changes before writing them.
Build an Expression Evaluator: a regex tokeniser feeding a recursive-descent parser.
Build a Source Code Analyser that reports on a Python file.
Build a Data Quality Report driven by one pattern per column.
Build a Validation Framework: rules as data, patterns as the engine.
Build a Template Engine with variables, loops and conditionals.
Explain every entry point in the re module: what each one returns, and when to reach for it.
You are handed a 200-character regex in a code review. Argue for or against it, with evidence.
Produce a complete, runnable reference for the syntax taught in this topic.
Make one badly-behaved pattern fast and safe, measuring every step.
Build a Text Intelligence Pipeline โ the complete demonstration of this topic. Ingest a messy document, extract structured records, validate them, redact what is sensitive, and print a full report.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session