Python Practice › RegEx Programs
Python RegEx Programs with Solutions
20 regular expression programs: validating emails, phone numbers, PIN codes and passwords, extracting numbers, dates, hashtags and URLs, and cleaning or reformatting text with re.sub.
Before you start
Read the RegEx lesson first if the topic is new to you.
All programs
- #1Validate email addresses
Write a Python program to check which strings are valid email addresses.Easy - #2Validate Indian mobile numbers
Write a Python program to validate 10-digit Indian mobile numbers with an optional +91.Easy - #3Validate PIN codes
Write a Python program to validate 6-digit Indian PIN codes (not starting with 0).Easy - #4Check a strong password
Write a Python program to check that a password has 8+ characters with upper, lower, digit and symbol.Medium - #5Extract all numbers
Write a Python program to extract integers and decimals from text.Easy - #6Extract dates
Write a Python program to extract dates in DD-MM-YYYY format and print them as YYYY-MM-DD.Easy - #7Extract hashtags and mentions
Write a Python program to extract hashtags and @mentions from a tweet.Easy - #8Extract URLs
Write a Python program to extract http and https URLs from text.Easy - #9Collapse extra spaces
Write a Python program to replace runs of whitespace with a single space.Easy - #10Remove special characters
Write a Python program to remove everything except letters, digits and spaces.Easy - #11Split on several delimiters
Write a Python program to split text on commas, semicolons and spaces.Easy - #12Match whole words only
Write a Python program to count the word “test” but not “testing” or “latest”.Easy - #13Parse log lines with named groups
Write a Python program to parse “LEVEL date message” log lines into dictionaries.Easy - #14Mask card numbers
Write a Python program to mask all but the last 4 digits of card numbers.Easy - #15camelCase to snake_case
Write a Python program to convert camelCase names to snake_case.Easy - #16Validate an IPv4 address
Write a Python program to check IPv4 addresses (each part 0-255).Easy - #17Find repeated words
Write a Python program to find accidentally repeated words like “the the”.Easy - #18Extract prices
Write a Python program to extract rupee prices like Rs 1,499.50 and add them up.Easy - #19Match positions with finditer
Write a Python program to print each match of a word with its start and end positions.Easy - #20Multiline and ignore-case flags
Write a Python program to find lines that start with “error” in any case.Easy