Practical Use Cases
Data Validation
Email Validation
import re
def validate_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return re.match(pattern, email) is not None
## LabEx email validation examples
emails = [
'[email protected]',
'invalid.email',
'[email protected]'
]
for email in emails:
print(f"{email}: {validate_email(email)}")
Phone Number Validation
def validate_phone(phone):
pattern = r'^\+?1?\d{10,14}$'
return re.match(pattern, phone) is not None
phones = ['+15551234567', '1234567890', 'invalid']
for phone in phones:
print(f"{phone}: {validate_phone(phone)}")
text = "Visit our website at https://www.labex.io and http://example.com"
urls = re.findall(r'https?://\S+', text)
print(urls)
Parsing Log Files
log_entry = "2023-06-15 14:30:45 [ERROR] Database connection failed"
pattern = r'(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}) \[(\w+)\] (.+)'
match = re.match(pattern, log_entry)
if match:
date, time, level, message = match.groups()
print(f"Date: {date}, Time: {time}, Level: {level}, Message: {message}")
Text Processing
def mask_sensitive_data(text):
## Mask email addresses
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
return re.sub(email_pattern, '[MASKED EMAIL]', text)
sample_text = "Contact support at [email protected] for assistance"
print(mask_sensitive_data(sample_text))
Configuration Parsing
Parsing Configuration Files
config = """
server_host=localhost
server_port=8080
debug_mode=true
"""
def parse_config(config_text):
config_dict = {}
pattern = r'^(\w+)=(.+)$'
for line in config_text.strip().split('\n'):
match = re.match(pattern, line)
if match:
key, value = match.groups()
config_dict[key] = value
return config_dict
parsed_config = parse_config(config)
print(parsed_config)
graph LR
A[Regex Use Cases] --> B[Data Validation]
A --> C[Data Extraction]
A --> D[Text Processing]
A --> E[Configuration Parsing]
Best Practices
Practice |
Description |
Example |
Compile Patterns |
Reuse compiled patterns |
pattern = re.compile(r'\d+') |
Use Raw Strings |
Prevent escape sequence issues |
r'\n' instead of '\\n' |
Handle Errors |
Catch potential regex exceptions |
try-except blocks |
Optimize Patterns |
Use specific, efficient patterns |
Avoid overly broad patterns |
import timeit
## Comparing regex vs string method performance
def regex_method():
re.search(r'\d+', 'Hello 123 World')
def string_method():
'123' in 'Hello 123 World'
## Measure execution time
regex_time = timeit.timeit(regex_method, number=10000)
string_time = timeit.timeit(string_method, number=10000)
print(f"Regex method time: {regex_time}")
print(f"String method time: {string_time}")