Practice Bank: Regular Expressions
A deeper set of practice problems, organized by topic, for extra reps
beyond the main exercises — including scenario-based problems
written in the same style you'll meet in real interviews. This is the
chapter where the re module's pattern matching
(match()/search()/fullmatch(),
findall()/finditer(), character classes,
quantifiers, anchors, groups & named groups,
re.sub()/re.split(), re.compile(),
greedy/non-greedy quantifiers, raw strings, and flags) becomes allowed,
on top of everything from Chapters 1-17 (including generators,
iterators, context managers, all OOP, exceptions, and file/CSV
handling). No import beyond
re/math/datetime/random/csv.
starter.py below and run
python3 starter.py from inside the same folder.
Topic 1: match()/search()/fullmatch()
1 Matching at the start of a string
Find # TODO 1.1. Use re.match() with the pattern r"\d+" on the string "42 apples" and print the resulting match's .group().
2 Matching anywhere in a string
Find # TODO 1.2. Use re.search() with the pattern r"\d+" on the string "there are 42 apples" and print the resulting match's .group().
3 Matching the entire string
Find # TODO 1.3. Use re.fullmatch() with the pattern r"\d+" on both "42" (should succeed) and "42 apples" (should fail). Print both results.
4 Checking a prefix across several words
Find # TODO 1.4. Loop over ["cat", "concatenate", "category"] and use re.match() with the pattern r"cat" on each word, printing whether it starts with "cat".
5 Debug the Code
Find # TODO 1.5. This uses re.match() where re.search() was actually needed, since the target text isn't at position 0. Fix it.
Validating a username field on a signup form
Find # TODO 1.A. Write is_valid_username(username) using re.fullmatch() to check the entire input is 3-16 letters/digits/underscores.
match() vs. search()
Find # TODO 1.B. Write explain_match_vs_search(), explaining the actual difference between re.match() and re.search(), and when each is the right choice.
Topic 2: findall() & finditer()
1 Extracting every dollar amount
Find # TODO 2.1. Use re.findall() with the pattern r"\$\d+" on "Items cost $5, $12, and $100" and print the result.
2 Getting each match's text and position
Find # TODO 2.2. Use re.finditer() with the pattern r"\w+at" on "cat hat bat mat", printing each match's .group() and .start().
3 Counting every word
Find # TODO 2.3. Use re.findall() with the pattern r"\b\w+\b" on "one two three", then print len(...) of the result.
4 Extracting every hashtag
Find # TODO 2.4. Use re.findall() with the pattern r"#\w+" on a string of social-media-style text, and print the result.
5 Debug the Code
Find # TODO 2.5. This pattern is missing a + quantifier, so multi-digit numbers split into single digits. Fix it.
Extracting every error code from a support ticket
Find # TODO 2.A. Write extract_error_codes(ticket_text) to pull every ERR-#### code out of free-text notes.
findall() vs. finditer()
Find # TODO 2.B. Write explain_findall_vs_finditer(), explaining when to reach for one over the other.
Topic 3: Character Classes, Quantifiers & Anchors
1 Counting vowels with a character class
Find # TODO 3.1. Use re.findall() with the pattern r"[aeiouAEIOU]" to count every vowel in a string.
2 An optional character with ?
Find # TODO 3.2. Use re.findall() with the pattern r"colou?r" to match both American and British spellings in one pattern.
3 An exact-count quantifier
Find # TODO 3.3. Use re.findall() with the pattern r"[A-Z]{2}\d{3}" to match a specific letter+digit code shape.
4 A range quantifier with anchors
Find # TODO 3.4. Use re.fullmatch() with the pattern r"^\d{3,5}$" to validate several strings.
5 Debug the Code
Find # TODO 3.5. This pattern has an unescaped literal decimal point, so it matches more than intended. Fix it.
Validating a simple password policy
Find # TODO 3.A. Write is_strong_password(password) requiring at least 8 letters/digits/underscores.
Quantifiers and anchors together
Find # TODO 3.B. Write explain_quantifiers_and_anchors(), explaining how they work together in a validation pattern.
Topic 4: Groups & Named Groups
1 Two numbered groups
Find # TODO 4.1. Use re.match() with the pattern r"(\w+)@(\w+)\.com" and print match.group(1)/match.group(2).
2 The same pieces as named groups
Find # TODO 4.2. Use re.match() with named groups (?P<user>...)/(?P<domain>...) and print .groupdict().
3 Every numbered group as a tuple
Find # TODO 4.3. Use re.match() with three groups on a timestamp string and print .groups().
4 Looping over multiple matches' groups
Find # TODO 4.4. Use re.finditer() with named groups, printing each match's .groupdict().
5 Debug the Code
Find # TODO 4.5. This named group is missing the required P in (?P<name>...). Fix it.
Parsing a "Last, First" name field
Find # TODO 4.A. Write split_last_first(full_name) using named groups to split a stored name field.
What problem do groups solve?
Find # TODO 4.B. Write explain_groups_purpose(), explaining what problem groups actually solve.
Topic 5: re.sub() & re.split()
1 Masking a credit card number
Find # TODO 5.1. Use re.sub() to mask a credit-card-shaped pattern with Xs.
2 Stripping punctuation and symbols
Find # TODO 5.2. Use re.sub() with a negated character class to strip out anything that isn't a letter, digit, or space.
3 Splitting on a flexible separator
Find # TODO 5.3. Use re.split() to split on a semicolon with optional surrounding whitespace.
4 Collapsing repeated characters
Find # TODO 5.4. Use re.sub() to collapse two-or-more repeated dashes down to a single dash.
5 Debug the Code
Find # TODO 5.5. This call to re.sub() has its arguments in the wrong order. Fix it.
Redacting Social Security Numbers from a document
Find # TODO 5.A. Write redact_ssns(document_text) to mask any SSN-shaped pattern before sharing a document.
Why not just use plain string methods?
Find # TODO 5.B. Write explain_sub_split_benefit(), explaining why re.sub()/re.split() beat fixed-text string methods here.
Topic 6: Compiled Patterns
1 A compiled digit pattern
Find # TODO 6.1. Build digit_pattern with re.compile() and call .findall() on it.
2 Scanning multiple lines with one compiled pattern
Find # TODO 6.2. Build a compiled email pattern and search several lines with it.
3 A compiled "long word" pattern
Find # TODO 6.3. Build a compiled pattern for words of 5 or more characters.
4 A compiled IP-address-shaped pattern
Find # TODO 6.4. Build a compiled pattern and collect matches across multiple lines into a list.
5 Debug the Code
Find # TODO 6.5. This code compiles a pattern but never actually uses the compiled object. Fix it.
Scanning many log lines for the same error pattern
Find # TODO 6.A. Write count_error_lines(log_lines) using a compiled pattern reused across every line.
When is compiling actually worth it?
Find # TODO 6.B. Write explain_compile_benefit(), explaining when compiling a pattern pays off.
Topic 7: Bringing It Together — Greedy Matching, Raw Strings & Flags
1 Greedy vs. non-greedy, side by side
Find # TODO 7.1. Compare a greedy .* match against a non-greedy .*? match on the same quoted text.
2 Confirming a raw-string pattern
Find # TODO 7.2. Build a raw-string pattern and confirm it matches version-number-shaped text.
3 Case-insensitive matching with a flag
Find # TODO 7.3. Use re.IGNORECASE to match regardless of capitalization.
4 Per-line anchoring with a flag
Find # TODO 7.4. Use re.MULTILINE so ^ anchors to each line instead of the whole string.
5 Debug the Code
Find # TODO 7.5. This greedy .* swallowed two separate quoted phrases into one match. Fix it.
A log-scanning tool combining everything
Find # TODO 7.A. Write scan_log_for_errors(log_text) combining a compiled pattern, named groups, and a flag.
The most common regex mistakes
Find # TODO 7.B. Write explain_regex_gotchas(), naming the most common regex mistakes beginners make.
solution.py. Your exact
wording doesn't need to match — the goal is that your program
runs without errors and does what each TODO asks.