Python RegEx Practice Exercises (Beginner → Advanced) with Complete Solutions
This article provides a complete set of Python Regular Expression (RegEx) Practice Exercises along with step-by-step solutions. The exercises are arranged from Beginner → Intermediate → Advanced → Bonus Challenge and now upgraded with more modern, realistic examples suitable for 2025 learning standards.
💡 Tip: Before solving, try each pattern yourself first using Python’s
remodule. Then compare with the solutions included later in this article.
A. Beginner Level Exercises
1. Extract Words Beginning with a Capital Letter
Input:
"Hello from OpenAI Research Team in San Francisco."
Expected Output:
['Hello', 'OpenAI', 'Research', 'Team', 'San', 'Francisco']
2. Extract All Numbers (Including Years, IDs, or Counts)
Input:
"Invoice ID 93421 was generated on 2025-03-16 with total 899 USD."
Expected Output:
['93421', '2025', '03', '16', '899']
3. Validate an Email Address with Optional Subdomains
Input:
"support.team@ai-platform.co.uk"
Expected Output:
True
4. Replace All Spaces and Tabs with a Single Underscore
Input:
"Python is evolving fast"
Expected Output:
"Python_is_evolving_fast"
5. Check If a Sentence Starts with a Particular Word (“Welcome”)
Input:
"Welcome to the Python Developer Community!"
Expected Output:
True
B. Intermediate Level Exercises
6. Extract All Hashtags (Including Underscore Tags)
Input:
"Trending topics: #Python, #AI_Research, and #DataScience2025!"
Expected Output:
['#Python', '#AI_Research', '#DataScience2025']
7. Validate U.S. Phone Numbers (Support Spaces or Hyphens)
Input:
"415-555-2671"
Expected Output:
True
8. Find Words Exactly 6 Letters Long
Input:
"People enjoy Python, coding, coffee, nature, and travel."
Expected Output:
['People', 'Python', 'coding', 'coffee', 'nature', 'travel']
C. Advanced Level Exercises
9. Extract Dates in DD-MM-YYYY Format (Hyphen Version)
Input:
"Events: 21-05-2025 kickoff, final meetup on 09-11-2024."
Expected Output:
['21-05-2025', '09-11-2024']
10. Remove HTML Tags Without Breaking Inner Text
Input:
"<div>Hello <strong>Developers</strong>!</div>"
Expected Output:
"Hello Developers!"
Bonus Challenge
11. Extract Valid IPv4 Addresses (0–255 Each Block)
Input:
"Active IPs: 10.0.0.1, 256.25.16.1, 172.20.10.5, 999.1.1.1"
Expected Output:
['10.0.0.1', '172.20.10.5']
✔ Complete Solutions to All Exercises
Import Module
import re
A. Beginner Level : Solutions
1. Words Starting with Capital Letters
text = "Hello from OpenAI Research Team in San Francisco."
result = re.findall(r'\b[A-Z][a-zA-Z]*\b', text)
print(result)
2. Extract All Numbers
text = "Invoice ID 93421 was generated on 2025-03-16 with total 899 USD."
numbers = re.findall(r'\d+', text)
print(numbers)
3. Validate Email Address
email = "support.team@ai-platform.co.uk"
pattern = r'^[\w\.-]+@[\w-]+(\.[\w-]+)+$'
print(bool(re.match(pattern, email)))
4. Replace Spaces & Tabs with Underscores
text = "Python is evolving fast"
cleaned = re.sub(r'\s+', '_', text)
print(cleaned)
5. String Starts with “Welcome”
text = "Welcome to the Python Developer Community!"
print(bool(re.match(r'^Welcome', text)))
B. Intermediate Level: Solutions
6. Extract Hashtags
text = "Trending topics: #Python, #AI_Research, and #DataScience2025!"
tags = re.findall(r'#\w+', text)
print(tags)
7. Validate US Phone Number
phone = "415-555-2671"
pattern = r'^\d{3}[- ]\d{3}[- ]\d{4}$'
print(bool(re.match(pattern, phone)))
8. Words With 6 Letters
text = "People enjoy Python, coding, coffee, nature, and travel."
words = re.findall(r'\b[A-Za-z]{6}\b', text)
print(words)
C. Advanced Level : Solutions
9. Extract DD-MM-YYYY Dates
text = "Events: 21-05-2025 kickoff, final meetup on 09-11-2024."
dates = re.findall(r'\b\d{2}-\d{2}-\d{4}\b', text)
print(dates)
10. Remove HTML Tags
html = "<div>Hello <strong>Developers</strong>!</div>"
clean_text = re.sub(r'<.*?>', '', html)
print(clean_text)
Bonus Challenge: Solution
11. Extract Valid IPv4 Addresses
text = "Active IPs: 10.0.0.1, 256.25.16.1, 172.20.10.5, 999.1.1.1"
pattern = (
r'\b(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)'
r'(?:\.(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3}\b'
)
valid_ips = re.findall(pattern, text)
print(valid_ips)
Final Thoughts
Regular expressions are a powerful tool for text processing, data cleaning, validation, and automation. With these upgraded exercises and solutions, you now have a practical and modern foundation for writing patterns used in real projects from data engineering to web scraping, AI pipelines, log analysis, and more.