๐งช Python RegEx Practice Exercises
✅ Instructions: Try to write a regular expression (using Python’s
re
module) that solves each problem. Test your solution with various inputs.
๐น Beginner Level
1. Find All Words Starting with Capital Letters
Input:
"Hello world. Python Is Amazing!"
Expected Output:
['Hello', 'Python', 'Is', 'Amazing']
2. Extract All Digits from a String
Input:
"Order #12345, shipped on 2024-11-10."
Expected Output:
['12345', '2024', '11', '10']
3. Check If a String Is a Valid Email
Input:
"example123@test-domain.com"
Expected Output:
True
if it's a valid email, else False
4. Replace All Whitespace with Underscores
Input:
"Python is fun and powerful"
Expected Output:
"Python_is_fun_and_powerful"
5. Check if a String Starts with “Hello”
Input:
"Hello, welcome to Python world!"
Expected Output:
True
๐น Intermediate Level
6. Extract All Hashtags from a Sentence
Input:
"Loving #Python and #AI in 2025!"
Expected Output:
['#Python', '#AI']
7. Validate a U.S. Phone Number
Input:
"123-456-7890"
Pattern:
Three digits, hyphen, three digits, hyphen, four digits
Expected Output:
True
8. Find All Words with Exactly 5 Letters
Input:
"These words: Apple, Mango, Zebra, and Orange are cool."
Expected Output:
['Apple', 'Mango', 'Zebra']
๐น Advanced Level
9. Extract Dates in DD/MM/YYYY Format
Input:
"Today's date is 21/05/2025. Another date is 01/01/2024."
Expected Output:
['21/05/2025', '01/01/2024']
10. Remove All HTML Tags from a String
Input:
"<p>Hello <b>World</b>!</p>"
Expected Output:
"Hello World!"
๐ง Bonus Challenge
11. Extract All Valid IPv4 Addresses
Input:
"These are IPs: 192.168.0.1, 10.0.0.256, 172.16.254.3"
Expected Output:
Only valid IP addresses (each number must be 0–255):
['192.168.0.1', '172.16.254.3']
๐ How to Use
To solve each problem:
-
Import the
re
module. -
Use
re.findall()
,re.match()
,re.search()
, orre.sub()
as needed. -
Test your pattern on various inputs.
Find answers/solutions for each exercise?
Perfect! Below are the solutions for each of the Python RegEx exercises provided earlier. These are written using the re
module and explained briefly so learners understand the logic behind them.
✅ Solutions to Python RegEx Exercises
๐ Import the module first:
import re
๐น Beginner Level
1. Find All Words Starting with Capital Letters
text = "Hello world. Python Is Amazing!"
result = re.findall(r'\b[A-Z][a-z]*\b', text)
print(result) # ['Hello', 'Python', 'Is', 'Amazing']
2. Extract All Digits from a String
text = "Order #12345, shipped on 2024-11-10."
digits = re.findall(r'\d+', text)
print(digits) # ['12345', '2024', '11', '10']
3. Check If a String Is a Valid Email
email = "example123@test-domain.com"
pattern = r'^[\w\.-]+@[\w\.-]+\.\w{2,}$'
print(bool(re.match(pattern, email))) # True
4. Replace All Whitespace with Underscores
text = "Python is fun and powerful"
new_text = re.sub(r'\s+', '_', text)
print(new_text) # Python_is_fun_and_powerful
5. Check if a String Starts with “Hello”
text = "Hello, welcome to Python world!"
print(bool(re.match(r'^Hello', text))) # True
๐น Intermediate Level
6. Extract All Hashtags from a Sentence
text = "Loving #Python and #AI in 2025!"
hashtags = re.findall(r'#\w+', text)
print(hashtags) # ['#Python', '#AI']
7. Validate a U.S. Phone Number
phone = "123-456-7890"
pattern = r'^\d{3}-\d{3}-\d{4}$'
print(bool(re.match(pattern, phone))) # True
8. Find All Words with Exactly 5 Letters
text = "These words: Apple, Mango, Zebra, and Orange are cool."
words = re.findall(r'\b\w{5}\b', text)
print(words) # ['Apple', 'Mango', 'Zebra']
๐น Advanced Level
9. Extract Dates in DD/MM/YYYY Format
text = "Today's date is 21/05/2025. Another date is 01/01/2024."
dates = re.findall(r'\b\d{2}/\d{2}/\d{4}\b', text)
print(dates) # ['21/05/2025', '01/01/2024']
10. Remove All HTML Tags from a String
html = "<p>Hello <b>World</b>!</p>"
clean_text = re.sub(r'<.*?>', '', html)
print(clean_text) # Hello World!
๐ง Bonus Challenge
11. Extract All Valid IPv4 Addresses
text = "These are IPs: 192.168.0.1, 10.0.0.256, 172.16.254.3"
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) # ['192.168.0.1', '172.16.254.3']
✅ This pattern ensures each octet is between 0 and 255.
No comments:
Post a Comment