当前位置: 首页 > article >正文

Python3 【正则表达式】项目实战:5 个学习案例

Python3 【正则表达式】项目实战:5 个学习案例

本文包含 5 个关于“正则表达式”的综合应用项目,每个项目都包含完整的程序代码、测试案例、执行结果以及代码说明。具体项目是:

  1. 邮箱地址提取器
  2. 日志文件分析器
  3. HTML 标签提取器
  4. 电话号码验证器
  5. 密码强度检查器

项目 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 处理、数据验证等。通过这些项目,可以更好地理解和掌握正则表达式的使用。


http://www.kler.cn/a/520268.html

相关文章:

  • 助记词登录时提示无效怎么办?
  • Windows10官方系统下载与安装保姆级教程【U盘-官方ISO直装】
  • salesforce中如何获取一个profile的18位id
  • HarmonyOS简介:HarmonyOS核心技术理念
  • 慢牛提速的内在逻辑-基本量价关系分析和主散心理博弈
  • 前端开发新趋势:使用 Zustand 打造轻量级状态管理
  • 一个简单的自适应html5导航模板
  • FPGA 使用 CLOCK_DEDICATED_ROUTE 约束
  • 【JS|第28期】new Event():前端事件处理的利器
  • Vue 3 中的响应式系统:ref 与 reactive 的对比与应用
  • OpenAI掀桌子!免费版ChatGPT,提供o3-mini模型!
  • 【故障诊断】量子粒子群优化极限学习机实现乳腺癌诊断,(QPSO-ELM)数据分类
  • 深度解析iTransformer:维度倒置与高效注意力机制的结合
  • vue3 react区别
  • C++——list的了解和使用
  • 美格智能AIMO智能体+DeepSeek-R1模型,AI应用的iPhone时刻来了
  • fantastic-admin5.0发布,Vue3 + django重构(一)后端
  • LeetCode题练习与总结:最短无序连续子数组--581
  • android的gradle
  • fiscoBcos中使用工具脚本在本地搭建一条4节点链