Python3 【正则表达式】项目实战:5 个学习案例
Python3 【正则表达式】项目实战:5 个学习案例
本文包含 5 个关于“正则表达式”的综合应用项目,每个项目都包含完整的程序代码、测试案例、执行结果以及代码说明。具体项目是:
- 邮箱地址提取器
- 日志文件分析器
- HTML 标签提取器
- 电话号码验证器
- 密码强度检查器
项目 1:邮箱地址提取器
功能描述
从一段文本中提取所有合法的邮箱地址。
代码
import re
def extract_emails(text):
# 正则表达式匹配邮箱地址
pattern = r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+'
# 查找所有匹配的邮箱地址
emails = re.findall(pattern, text)
return emails
# 测试案例
text = """
Contact us at support@example.com or sales@example.org.
For more information, visit https://www.example.com.
Invalid email: user@com.
"""
emails = extract_emails(text)
print("Extracted emails:", emails)
执行结果
Extracted emails: ['support@example.com', 'sales@example.org']
代码说明
- 使用正则表达式
[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+
匹配邮箱地址。 re.findall
返回所有匹配的邮箱地址。
项目 2:日志文件分析器
功能描述
从日志文件中提取所有错误日志(包含 “ERROR” 关键字)。
代码
import re
def extract_errors(log_file):
# 正则表达式匹配错误日志
pattern = r'ERROR.*'
errors = []
with open(log_file, 'r') as file:
for line in file:
match = re.search(pattern, line)
if match:
errors.append(match.group())
return errors
# 测试案例
log_file = 'sample_log.txt'
# 假设 sample_log.txt 内容如下:
"""
INFO: User logged in
ERROR: Failed to connect to database
INFO: Request processed
ERROR: File not found
"""
errors = extract_errors(log_file)
print("Error logs:")
for error in errors:
print(error)
执行结果
Error logs:
ERROR: Failed to connect to database
ERROR: File not found
代码说明
- 使用正则表达式
ERROR.*
匹配以 “ERROR” 开头的日志行。 - 逐行读取日志文件并提取匹配的错误日志。
项目 3:HTML 标签提取器
功能描述
从 HTML 文本中提取所有标签及其内容。
代码
import re
def extract_html_tags(html):
# 正则表达式匹配 HTML 标签及其内容
pattern = r'<(\w+)[^>]*>(.*?)</\1>'
tags = re.findall(pattern, html)
return tags
# 测试案例
html = """
<div class="header">Welcome</div>
<p>This is a <b>test</b> paragraph.</p>
<a href="https://example.com">Link</a>
"""
tags = extract_html_tags(html)
print("HTML tags and content:")
for tag, content in tags:
print(f"Tag: {tag}, Content: {content}")
执行结果
HTML tags and content:
Tag: div, Content: Welcome
Tag: p, Content: This is a <b>test</b> paragraph.
Tag: a, Content: Link
代码说明
- 使用正则表达式
<(\w+)[^>]*>(.*?)</\1>
匹配 HTML 标签及其内容。 re.findall
返回所有匹配的标签及其内容。
项目 4:电话号码验证器
功能描述
验证输入的电话号码是否符合中国大陆的手机号码格式。
代码
import re
def validate_phone(phone):
# 正则表达式匹配中国大陆手机号码
pattern = r'^1[3-9]\d{9}$'
if re.match(pattern, phone):
return True
return False
# 测试案例
phones = ["13800138000", "12345678901", "19912345678"]
for phone in phones:
if validate_phone(phone):
print(f"{phone} is valid.")
else:
print(f"{phone} is invalid.")
执行结果
13800138000 is valid.
12345678901 is invalid.
19912345678 is valid.
代码说明
- 使用正则表达式
^1[3-9]\d{9}$
匹配中国大陆手机号码。 re.match
检查字符串是否符合格式。
项目 5:密码强度检查器
功能描述
检查密码是否符合强密码规则(至少 8 位,包含大小写字母、数字和特殊字符)。
代码
import re
def check_password_strength(password):
# 正则表达式匹配强密码
pattern = r'^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[\W_]).{8,}$'
if re.match(pattern, password):
return True
return False
# 测试案例
passwords = ["Password123!", "weakpass", "StrongPass1"]
for pwd in passwords:
if check_password_strength(pwd):
print(f"'{pwd}' is strong.")
else:
print(f"'{pwd}' is weak.")
执行结果
'Password123!' is strong.
'weakpass' is weak.
'StrongPass1' is strong.
代码说明
- 使用正则表达式
^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[\W_]).{8,}$
匹配强密码。 re.match
检查密码是否符合规则。
总结
以上 5 个迷你项目展示了正则表达式在实际应用中的强大功能,包括文本提取、日志分析、HTML 处理、数据验证等。通过这些项目,可以更好地理解和掌握正则表达式的使用。