Python · Intermediate Python

Regular Expressions — Practice Questions

50 questions. Try each one yourself before checking the answer.

Q1re.match()Easy

Import re and use re.match() to check if "Hello World" STARTS WITH "Hello", printing whether a match was found.

Q2re.search()Easy

Use re.search() to find "World" ANYWHERE in "Hello World", even though it's not at the start.

Q3re.match() vs re.search()Easy

Show that re.match("World", "Hello World") fails (returns None) while re.search("World", "Hello World") succeeds, explaining the difference in a comment.

Q4re.findall()Easy

Use re.findall() to find every digit in "Room 42, Building 7".

Q5\d+Easy

Use re.findall() with \d+ (one or more digits) to find WHOLE numbers in "Room 42, Building 7", not individual digits.

Q6Character ClassesEasy

Use re.findall() with a character class [aeiou] to find every vowel in "hello world".

Q7\w and \sEasy

Use re.findall() with \w+ to split "the quick brown fox" into its word tokens.

Q8QuantifiersEasy

Use re.findall() with \d{3} to find every group of EXACTLY 3 consecutive digits in "12 345 6789".

Q9re.fullmatch()Easy

Use re.fullmatch() to check whether "12345" is made up ENTIRELY of digits (the whole string, not just part of it).

Q10re.compile()Easy

Compile a pattern for digits using re.compile(), then use the compiled pattern object to .findall() on a string.

Q11GroupsMedium

Use re.search() with a capturing group (\d+) to extract just the number from "Order #4521", using .group(1).

Q12Named GroupsMedium

Use a NAMED group (?P<order_id>\d+) to extract the same order number, accessed via .group("order_id").

Q13re.split()Medium

Use re.split() to split "one,two;three four" on ANY of ,, ;, or a space, using a character class in the pattern.

Q14re.sub()Medium

Use re.sub() to replace every digit in "Room 42, Building 7" with "#".

Q15Quantifier RangesMedium

Use \d{2,4} to find number groups that are 2 to 4 digits long in "1 23 456 7890".

Q16AnchorsMedium

Use ^ and $ anchors to check whether "hello" matches ONLY when it's the entire line, demonstrating with both a matching and non-matching example.

Q17Word BoundaryMedium

Use \bcat\b to find "cat" as a WHOLE WORD in "the cat sat, concatenate", showing it correctly skips the cat inside "concatenate".

Q18AlternationMedium

Use | (alternation) to find every occurrence of either "cat" or "dog" in "I have a cat and a dog".

Q19FlagsMedium

Use re.IGNORECASE to match "python" regardless of case in "I love Python programming".

Q20Multiple GroupsMedium

Given "2024-06-15", use re.findall() with THREE groups (\d+)-(\d+)-(\d+) to extract year, month, day as a tuple.

Q21Real-WorldMedium

Write a basic email validator: check whether a string matches something@something.something using re.fullmatch().

Q22Real-WorldMedium

Extract all phone numbers in the format XXX-XXX-XXXX from "Call 987-654-3210 or 123-456-7890".

Q23Real-WorldMedium

Write a password strength check requiring at least 8 characters and at least one digit, using re.search().

Q24Real-WorldMedium

Extract every hashtag (#word) from "Loving #Python and #Coding today!".

Q25Real-WorldMedium

Extract ALL numbers (including decimals) from "The total is 45.99 with 3 items at 15.33 each".

Q26Real-WorldMedium

Validate a date in DD-MM-YYYY format using re.fullmatch() with a pattern that ensures 2 digits, 2 digits, and 4 digits.

Q27Real-WorldMedium

Split "apple, banana ,cherry, date" on commas, allowing for inconsistent spacing around them, and strip the results.

Q28Real-WorldMedium

Mask all but the last 4 digits of a credit card number "4111111111111111" using re.sub().

Q29Real-WorldMedium

Extract the domain from "https://www.jrcodex.com/articles/python" using a capturing group.

Q30FormattingMedium

Given "John:25, Alice:30, Bob:22", use re.findall() with two groups to extract (name, age) pairs, then print them formatted.

Q31Nested GroupsHard

Given "2024-06-15T14:30:00", use nested groups to separately capture the date part and the time part, then further split the date into year/month/day.

Q32LookaheadHard

Use a positive lookahead (?=...) to find every word that is IMMEDIATELY followed by ":" in "name: Alice, age: 30", without including the colon in the match.

Q33LookbehindHard

Use a positive lookbehind (?<=...) to extract the price digits that come immediately AFTER a $ sign in "Total: $45.99, Tax: $3.60", without including the $ itself.

Q34Greedy vs Non-GreedyHard

Given "<b>bold</b> and <i>italic</i>", show the difference between a GREEDY <.+> match and a NON-GREEDY <.+?> match.

Q35re.sub() with FunctionHard

Use re.sub() with a FUNCTION (not a string) as the replacement to double every number found in "I have 3 apples and 5 oranges".

Q36re.finditer()Hard

Use re.finditer() to find every number in a string AND print each match's text alongside its start/end position.

Q37Combined ValidationHard

Write a strong-password validator combining FOUR lookaheads: must contain a digit, an uppercase letter, a lowercase letter, and a special character, all in one re.fullmatch() pattern.

Q38Real-WorldHard

Given "name=Alice;age=30;city=Pune", extract every key-value pair into a dictionary using re.findall() with two groups.

Q39Multiline FlagHard

Given a multi-line string, use re.MULTILINE so ^ and $ match the start/end of EACH LINE rather than just the whole string.

Q40DebuggingHard

The following code is meant to validate a US ZIP code (5 digits) but incorrectly accepts "123456" too. Find and fix the bug.

import re
 
def is_valid_zip(zip_code):
    return re.match(r"\d{5}", zip_code) is not None
 
print(is_valid_zip("123456"))
Q41Mini-ProjectMini-Project

Build a "Contact Extractor". Given a block of text, extract every email address AND every phone number it contains.

Q42Mini-ProjectMini-Project

Build a "Log Parser". Given log lines like "2024-06-15 14:30:00 ERROR Failed to connect", use named groups to extract date, time, level, and message.

Q43Mini-ProjectMini-Project

Build a "Form Validator" checking email, phone, and ZIP code fields, all with their own regex pattern, printing a validation report.

Q44Mini-ProjectMini-Project

Build a "URL Parser" that extracts the protocol, domain, and path from a URL using named groups.

Q45Mini-ProjectMini-Project

Build a "Password Strength Checker" that reports separately whether a password has enough length, a digit, an uppercase letter, and a special character (each as its own boolean, not just pass/fail).

Q46InterviewInterview

Explain the precise difference between re.match(), re.search(), and re.fullmatch(), demonstrating all three against the same string and pattern.

Q47InterviewInterview

Explain greedy vs non-greedy quantifiers with a real-world consequence: parsing simple HTML-like tags, showing how greedy matching can silently swallow more than intended.

Q48InterviewInterview

Explain why precompiling a pattern with re.compile() matters when the SAME pattern is used repeatedly (e.g. inside a loop validating thousands of rows), demonstrating with timeit.

Q49InterviewInterview

Explain what "catastrophic backtracking" is at a high level, and demonstrate a SAFER pattern rewrite that avoids nested unbounded quantifiers on overlapping character sets.

Q50CapstoneInterview

Build a "Regex Mastery Report" — the most complete demonstration in this module. Given a block of mixed text, extract: all emails, all phone numbers, all hashtags, all monetary amounts (with $), and validate one password — printing everything in a labeled report.

Still stuck on something?

Book a free 1-on-1 session and we'll work through it together.

Book a Free Session