您的位置:首页 >Python 正则表达式不区分大小写可以通过在 re 模块的函数中使用 re.IGNORECASE 标志来实现。以下是几种常见方式:✅ 方法一:使用 re.IG
发布于2025-10-08 阅读(0)
扫一扫,手机访问
使用re.IGNORECASE或re.I标志可实现不区分大小写的正则匹配,如re.findall(r'python', text, re.I)能匹配'Python'、'python'和'PYTHON'。

在Python中使用正则表达式时,若要实现不区分大小写匹配,可以通过设置标志参数 re.IGNORECASE 或简写为 re.I 来实现。
import re
text = "Python is great. I love python. PYTHON rocks!"
matches = re.findall(r'python', text, re.IGNORECASE)
print(matches) # 输出: ['Python', 'python', 'PYTHON']
matches = re.findall(r'python', text, re.I)
print(matches) # 同样输出: ['Python', 'python', 'PYTHON']
pattern = re.compile(r'python', re.IGNORECASE)
matches = pattern.findall(text)
print(matches) # 输出: ['Python', 'python', 'PYTHON']
text = """Python
pyTHON
PYTHON"""
matches = re.findall(r'^python$', text, re.IGNORECASE | re.MULTILINE)
print(matches) # 匹配每一行的 "python"(不区分大小写)
基本上就这些。只要加上 re.IGNORECASE 或 re.I,就能轻松实现不区分大小写的正则匹配。
上一篇:PS教程:灭霸特效制作教程
下一篇:海马汽车绑定爱车管理方法详解
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
9