Regular Expressions — Practice Questions
50 questions. Try each one yourself before checking the answer.
Import re and use re.match() to check if "Hello World" STARTS WITH "Hello", printing whether a match was found.
Use re.search() to find "World" ANYWHERE in "Hello World", even though it's not at the start.
Show that re.match("World", "Hello World") fails (returns None) while re.search("World", "Hello World") succeeds, explaining the difference in a comment.
Use re.findall() to find every digit in "Room 42, Building 7".
Use re.findall() with \d+ (one or more digits) to find WHOLE numbers in "Room 42, Building 7", not individual digits.
Use re.findall() with a character class [aeiou] to find every vowel in "hello world".
Use re.findall() with \w+ to split "the quick brown fox" into its word tokens.
Use re.findall() with \d{3} to find every group of EXACTLY 3 consecutive digits in "12 345 6789".
Use re.fullmatch() to check whether "12345" is made up ENTIRELY of digits (the whole string, not just part of it).
Compile a pattern for digits using re.compile(), then use the compiled pattern object to .findall() on a string.
Use re.search() with a capturing group (\d+) to extract just the number from "Order #4521", using .group(1).
Use a NAMED group (?P<order_id>\d+) to extract the same order number, accessed via .group("order_id").
Use re.split() to split "one,two;three four" on ANY of ,, ;, or a space, using a character class in the pattern.
Use re.sub() to replace every digit in "Room 42, Building 7" with "#".
Use \d{2,4} to find number groups that are 2 to 4 digits long in "1 23 456 7890".
Use ^ and $ anchors to check whether "hello" matches ONLY when it's the entire line, demonstrating with both a matching and non-matching example.
Use \bcat\b to find "cat" as a WHOLE WORD in "the cat sat, concatenate", showing it correctly skips the cat inside "concatenate".
Use | (alternation) to find every occurrence of either "cat" or "dog" in "I have a cat and a dog".
Use re.IGNORECASE to match "python" regardless of case in "I love Python programming".
Given "2024-06-15", use re.findall() with THREE groups (\d+)-(\d+)-(\d+) to extract year, month, day as a tuple.
Write a basic email validator: check whether a string matches something@something.something using re.fullmatch().
Extract all phone numbers in the format XXX-XXX-XXXX from "Call 987-654-3210 or 123-456-7890".
Write a password strength check requiring at least 8 characters and at least one digit, using re.search().
Extract every hashtag (#word) from "Loving #Python and #Coding today!".
Extract ALL numbers (including decimals) from "The total is 45.99 with 3 items at 15.33 each".
Validate a date in DD-MM-YYYY format using re.fullmatch() with a pattern that ensures 2 digits, 2 digits, and 4 digits.
Split "apple, banana ,cherry, date" on commas, allowing for inconsistent spacing around them, and strip the results.
Mask all but the last 4 digits of a credit card number "4111111111111111" using re.sub().
Extract the domain from "https://www.jrcodex.com/articles/python" using a capturing group.
Given "John:25, Alice:30, Bob:22", use re.findall() with two groups to extract (name, age) pairs, then print them formatted.
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.
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.
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.
Given "<b>bold</b> and <i>italic</i>", show the difference between a GREEDY <.+> match and a NON-GREEDY <.+?> match.
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".
Use re.finditer() to find every number in a string AND print each match's text alongside its start/end position.
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.
Given "name=Alice;age=30;city=Pune", extract every key-value pair into a dictionary using re.findall() with two groups.
Given a multi-line string, use re.MULTILINE so ^ and $ match the start/end of EACH LINE rather than just the whole string.
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"))Build a "Contact Extractor". Given a block of text, extract every email address AND every phone number it contains.
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.
Build a "Form Validator" checking email, phone, and ZIP code fields, all with their own regex pattern, printing a validation report.
Build a "URL Parser" that extracts the protocol, domain, and path from a URL using named groups.
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).
Explain the precise difference between re.match(), re.search(), and re.fullmatch(), demonstrating all three against the same string and pattern.
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.
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.
Explain what "catastrophic backtracking" is at a high level, and demonstrate a SAFER pattern rewrite that avoids nested unbounded quantifiers on overlapping character sets.
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