15.Python正则表达式入门:掌握文本处理的利器

正则表达式(Regular Expression)是处理文本数据的强大工具。在Python中,re模块提供了完整的正则表达式功能。本文将介绍5个核心方法及其应用场景,助您快速上手。


一、正则表达式快速入门

正则表达式通过特殊字符组合实现以下功能:

  • 文本搜索
  • 数据验证
  • 字符串替换
  • 数据提取

基础元字符:

  • . 匹配任意字符(除换行符)
  • \d 匹配数字
  • \w 匹配字母/数字/下划线
  • ^ 匹配字符串开头
  • $ 匹配字符串结尾
  • * 匹配0次或多次
  • + 匹配1次或多次
  • ? 匹配0次或1次

二、Python re模块核心方法

1. re.match() - 字符串开头匹配

作用 :从字符串起始位置匹配模式
返回值:成功返回Match对象,失败返回None

python 复制代码
import re

pattern = r"Hello"
text = "Hello World"

result = re.match(pattern, text)
if result:
    print("Match found:", result.group())  # 输出: Hello
else:
    print("No match")

2. re.search() - 全局搜索匹配

作用:扫描整个字符串寻找第一个匹配项

python 复制代码
text = "Python version: 3.11.5"
pattern = r"\d+\.\d+\.\d+"

result = re.search(pattern, text)
print(result.group())  # 输出: 3.11.5

3. re.findall() - 查找所有匹配

作用:返回所有非重叠匹配的字符串列表

python 复制代码
text = "Emails: user@test.com, admin@demo.org"
pattern = r"\w+@\w+\.\w+"

emails = re.findall(pattern, text)
print(emails)  # ['user@test.com', 'admin@demo.org']

4. re.sub() - 字符串替换

作用:替换匹配内容

python 复制代码
text = "2023-08-15"
new_text = re.sub(r"-", "/", text)
print(new_text)  # 2023/08/15

5. re.split() - 模式分割

作用:根据模式分割字符串

python 复制代码
text = "Apple,Banana;Cherry|Date"
items = re.split(r"[,;|]", text)
print(items)  # ['Apple', 'Banana', 'Cherry', 'Date']

三、高级技巧

1. 分组提取

使用()捕获特定内容

python 复制代码
text = "Phone: 010-12345678"
pattern = r"(\d{3})-(\d{4,8})"

match = re.search(pattern, text)
if match:
    print("区号:", match.group(1))  # 010
    print("号码:", match.group(2))  # 12345678

2. 预编译正则表达式

提升重复使用效率

python 复制代码
pattern = re.compile(r"\b[A-Z]+\b")
text = "THIS is a TEST"
matches = pattern.findall(text)
print(matches)  # ['THIS', 'TEST']

3. 非贪婪匹配

使用?进行最小匹配

python 复制代码
text = "<div>content</div><p>text</p>"
matches = re.findall(r"<.*?>", text)
print(matches)  # ['<div>', '</div>', '<p>', '</p>']

四、注意事项

  1. 原始字符串 :始终使用r""前缀避免转义问题
  2. 匹配优先级 :
    • match()只匹配开头
    • search()匹配第一个出现
    • findall()返回所有匹配
  3. 特殊字符 :需要转义的字符:\.^$*+?{}[]|()

五、实战应用场景

  1. 数据清洗
python 复制代码
# 移除非数字字符
dirty_data = "Price: $12,345.67"
clean = re.sub(r"[^\d.]", "", dirty_data)
print(clean)  # 12345.67
  1. 日志分析
python 复制代码
log = "ERROR [2023-08-15 14:23:45] Connection timeout"
pattern = r"(ERROR|WARN) \[(.*?)\] (.*)"
match = re.search(pattern, log)
  1. 表单验证
python 复制代码
def validate_email(email):
    pattern = r"^[\w\.-]+@[\w\.-]+\.\w+$"
    return re.match(pattern, email) is not None

总结

正则表达式是每个Python开发者必备的技能。掌握这些方法后,您可以: ✅ 快速处理文本数据

✅ 实现复杂格式验证

✅ 高效提取关键信息

进阶建议:

  • 学习更多元字符(如\s、\b)
  • 了解正则表达式修饰符(如re.IGNORECASE)
  • 练习复杂模式匹配

官方文档参考:re模块文档


通过这篇指南,您已经掌握了Python正则表达式的核心使用方法。现在可以尝试用正则表达式解决实际工作中的文本处理问题啦!

相关推荐
志尊宝10 小时前
AI原生前端开发(043):AI辅助的样式生成(Tailwind CSS)
前端·css·ai-native
mumu15710 小时前
快速上手大模型推理vllm
人工智能·python·大语言模型·agent·vllm
明天会有多晴朗10 小时前
从零手写 Transformer!一个 Python 文件跑通英译中(PyTorch 附完整源码)
人工智能·pytorch·python·深度学习·transformer
king王一帅10 小时前
QuireKit 编辑器的 AI-Native 是如何设计的?谈谈我对真正 AI-Native 的理解
前端·开源·agent
风骏时光牛马11 小时前
生成一个AI开源工具体验标题,只需要一个标题,不需要其他的任何文本。
前端
软件派12 小时前
生产动态数据智能风控:阈值判别、趋势预测、异常识别算法工程落地实战
前端·数据库·算法
ragnwang19 小时前
Notes: Python pybind11 import C++ module
开发语言·c++·python
王中阳Go19 小时前
后端转 Agent 岗面试怎么答:8 个高频问题与应答口径
人工智能·后端·python·大语言模型
花开盛夏^.^20 小时前
电脑打开多个浏览器Web窗口占用较大的运行内存的原因
前端·浏览器
weixin_3077791320 小时前
RAG 面试陷阱——混合检索并非无条件优越:一个条件反转实验
人工智能·python·面试·职场和发展