上一篇
Python findall函数匹配字符串教程 - 详解与示例
- Python
- 2025-07-21
- 62
Python findall()函数完全指南
掌握正则表达式的强大字符串匹配能力
Python的re.findall()函数是正则表达式模块中极其有用的字符串匹配工具。它可以快速搜索字符串中所有符合特定模式的子串,并以列表形式返回结果。本教程将带你全面了解findall()的使用方法、常见技巧和实际应用场景。
什么是findall()函数?
re.findall()是Python re模块中的一个函数,用于查找字符串中所有匹配正则表达式模式的非重叠出现,并将结果作为字符串列表返回。
函数语法
re.findall(pattern, string, flags=0)
- pattern: 要匹配的正则表达式
- string: 要搜索的原始字符串
- flags: 可选参数,用于修改正则表达式的匹配方式
基本用法示例
示例1:查找所有数字
import re
text = "订单号: 12345, 金额: ¥599.00, 日期: 2023-08-15"
numbers = re.findall(r'\d+', text)
print(numbers)
输出结果: ['12345', '599', '00', '2023', '08', '15']
示例2:查找特定单词
text = "Python is powerful. Python is easy to learn. Python is versatile."
matches = re.findall(r'Python', text, re.IGNORECASE)
print(matches)
输出结果: ['Python', 'Python', 'Python']
核心功能解析
1. 使用分组
当正则表达式中包含分组时,findall()会返回元组列表,每个元组对应一个匹配中的分组内容。
分组匹配示例
text = "姓名: 张三, 电话: 13800138000; 姓名: 李四, 电话: 13900139000"
matches = re.findall(r'姓名: (\w+), 电话: (\d{11})', text)
print(matches)
输出结果: [('张三', '13800138000'), ('李四', '13900139000')]
2. 非贪婪匹配
使用*?
或+?
实现非贪婪匹配,获取尽可能短的匹配结果。
贪婪 vs 非贪婪匹配
html = "<div>标题</div><div>内容</div>"
# 贪婪匹配
greedy = re.findall(r'<div>.*</div>', html)
print("贪婪匹配:", greedy)
# 非贪婪匹配
non_greedy = re.findall(r'<div>.*?</div>', html)
print("非贪婪匹配:", non_greedy)
输出结果:
贪婪匹配: ['<div>标题</div><div>内容</div>']
非贪婪匹配: ['<div>标题</div>', '<div>内容</div>']
贪婪匹配: ['<div>标题</div><div>内容</div>']
非贪婪匹配: ['<div>标题</div>', '<div>内容</div>']
3. 常用正则表达式模式
模式 | 描述 | 示例 |
---|---|---|
\d | 匹配任何数字 | findall(r'\d', 'a1b2c3') → ['1','2','3'] |
\w | 匹配字母、数字或下划线 | findall(r'\w', 'user_name@123') → ['u','s','e','r','_','n','a','m','e','1','2','3'] |
\s | 匹配空白字符 | findall(r'\s', 'a b c') → [' ', ' '] |
. | 匹配除换行符外的任意字符 | findall(r'a.c', 'abc adc a c') → ['abc','adc','a c'] |
[abc] | 匹配括号内的任意字符 | findall(r'[aeiou]', 'hello') → ['e','o'] |
^ | 匹配字符串开头 | findall(r'^\d', '1abc 2def') → ['1'] |
$ | 匹配字符串结尾 | findall(r'\d$', 'abc 123') → ['3'] |
实用案例展示
📧 提取所有邮箱地址
text = "联系我们: support@example.com 或 sales@company.net"
emails = re.findall(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', text)
print(emails)
输出结果: ['support@example.com', 'sales@company.net']
🔗 提取URL链接
html = '<a href="https://example.com">链接1</a> <a href="/about">链接2</a>'
urls = re.findall(r'href="([^"]*)"', html)
print(urls)
输出结果: ['https://example.com', '/about']
📅 提取日期
text = "会议日期: 2023-12-15, 截止日期: 2024-01-31"
dates = re.findall(r'\d{4}-\d{2}-\d{2}', text)
print(dates)
输出结果: ['2023-12-15', '2024-01-31']
💰 提取货币金额
text = "价格: $19.99, 折扣价: €15.50, 总计: ¥100.00"
prices = re.findall(r'[$€¥](\d+\.\d{2})', text)
print(prices)
输出结果: ['19.99', '15.50', '100.00']
注意事项
重要提示:
- 当正则表达式包含多个分组时,返回的是元组列表
- 如果没有匹配项,findall()返回空列表而非None
- 对于大型文本,考虑使用finditer()以节省内存
- 复杂的正则表达式可能影响性能,需进行优化
- 注意特殊字符的转义(如. * + ?等)
本文由LuCong于2025-07-21发表在吾爱品聚,如有疑问,请联系我们。
本文链接:https://521pj.cn/20256158.html
发表评论