Practice: Regular Expressions

« Read the chapter

Q1. Find the first number

BeginnerCoding

Use re.search to find the first run of digits in the text 'Room 42 on floor 3' and print it.

Goal: Use re.search with a digit pattern.

Q2. List all numbers

BeginnerCoding

Print a list of all digit groups found in 'a1 b22 c333'.

Goal: Use findall to collect all matches.

Q3. Replace spaces

BeginnerCoding

Replace every run of whitespace in 'one two three' with a single dash and print the result.

Goal: Use re.sub with \s+.

Q4. Validate a postal code

IntermediateCoding

Check each of '12345', '1234' and '12a45' against a rule of exactly five digits. Print the code and True or False.

Goal: Validate whole strings with fullmatch.

Q5. Extract the domain

IntermediateCoding

From 'write to asha@example.com today' print only the domain part after the @ sign.

Goal: Use a capture group.

Q6. Reorder a date

IntermediateCoding

Turn '2025-12-31' into '31.12.2025' using re.sub and group references.

Goal: Reuse captured groups in a replacement.

Q7. Split on separators

IntermediateCoding

Split 'a, b;c; d' on commas or semicolons followed by optional spaces and print the list.

Goal: Use re.split with a character class.

Q8. Count the vowels

AdvancedCoding

Count the vowels in 'Regular Expressions' ignoring case, using the re module.

Goal: Combine a class with a flag.

Q9. What does search return?

BeginnerOutput

What does this program print?

Code
import re

print(re.search(r"b+", "aabbbcc").group())

Goal: Read simple patterns.

Q10. Greedy match

IntermediateOutput

What does this program print?

Code
import re

print(re.findall(r"a.+b", "a1b a2b"))

Goal: Predict greedy behaviour.

Q11. Lazy match

IntermediateOutput

What does this program print?

Code
import re

print(re.findall(r"a.+?b", "a1b a2b"))

Goal: Predict lazy behaviour.

Q12. match versus search

BeginnerOutput

What does this program print?

Code
import re

print(re.match(r"\d+", "abc123"))
print(re.search(r"\d+", "abc123").group())

Goal: Distinguish match from search.

Q13. Group tuples

IntermediateOutput

What does this program print?

Code
import re

print(re.findall(r"(\w)=(\d)", "a=1, b=2"))

Goal: Predict findall with groups.

Q14. Named group dictionary

AdvancedOutput

What does this program print?

Code
import re

m = re.fullmatch(r"(?P<h>\d+):(?P<m>\d+)", "9:45")
print(m.groupdict())

Goal: Read named groups.

Q15. Why does this crash?

BeginnerDebugging

This program crashes with an AttributeError. Fix it so it prints 'none found' when there is no digit.

Code
import re

m = re.search(r"\d+", "no digits")
print(m.group())

Goal: Handle a None result.

Q16. Unescaped dot

IntermediateDebugging

This prints True for 3x14 but we want False. Fix the pattern.

Code
import re

print(bool(re.fullmatch(r"3.14", "3x14")))

Goal: Escape special characters.

Q17. Pick the right function

BeginnerConceptual

You must check that a whole string is a valid 4-digit PIN. Which re function is best and why?

Goal: Choose between match, search and fullmatch.

Q18. Why raw strings?

BeginnerConceptual

Explain why regex patterns in Python are usually written as raw strings.

Goal: Explain raw strings.

Q19. Extract hashtags

IntermediateApplication

From the post 'Loving #python and #coding today! #fun' print the list of hashtag words without the # sign.

Goal: Extract data from social text.

Q20. Mask phone digits

AdvancedApplication

Replace every digit of '9876543210' with * except the last four digits.

Goal: Hide private data with lookahead.