10 Handy Python Regular Expression Scripts for Common Tasks
This article presents ten concise Python regular‑expression scripts that demonstrate how to validate email addresses, extract URLs, replace numbers, assess password strength, split strings, match dates, strip HTML tags, capitalize words, find phone numbers, and replace specific characters, empowering developers with practical pattern‑matching techniques.
Hey friends! Today we explore the magic of Python regular expressions with ten concise scripts that solve common coding problems.
1. Validate email address
import re
def validate_email(email):
pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
if re.match(pattern, email):
return True
else:
return False
print(validate_email('[email protected]')) # Output: True2. Extract URLs from HTML
html_content = '''
Example
'''
urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', html_content)
print(urls) # Output: ['http://www.example.com']3. Replace numbers in text
text = "The price is $100."
new_text = re.sub(r'\$\d+', '$XXX', text)
print(new_text) # Output: The price is $XXX.4. Check password strength
password = 'Password123!'
def check_password_strength(password):
if len(password) < 8:
return False
elif not re.search("[a-z]", password):
return False
elif not re.search("[A-Z]", password):
return False
elif not re.search("[0-9]", password):
return False
elif not re.search("[_@$]", password):
return False
else:
return True
print(check_password_strength(password)) # Output: True5. Split a string
text = "one,two,three,four"
words = re.split(',', text)
print(words) # Output: ['one', 'two', 'three', 'four']6. Match date format
date_string = "2023-03-15"
def match_date(date):
pattern = r'\d{4}-\d{2}-\d{2}'
if re.match(pattern, date):
return True
else:
return False
print(match_date(date_string)) # Output: True7. Remove HTML tags
html = "Hello, world!"
clean_text = re.sub('<[^<]+?>', '', html)
print(clean_text) # Output: Hello, world!8. Capitalize first letters of words
text = "hello World from python"
capitalized_words = re.findall(r'\b[A-Z][a-z]*', text)
print(capitalized_words) # Output: ['World']9. Find phone numbers
text = "Contact us at +1 (123) 456-7890 or 123-456-7890."
def find_phone_numbers(text):
pattern = r'\+?1?\s?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}'
return re.findall(pattern, text)
print(find_phone_numbers(text)) # Output: ['+1 (123) 456-7890', '123-456-7890']10. Replace specific characters
text = "Hello, World!"
new_text = re.sub('[,]', ' ', text)
print(new_text) # Output: Hello World!These scripts are just the tip of the iceberg; regular expressions offer far more power. Keep practicing to master them, and don’t forget to like, share, and follow for more programming tips!
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.