做网站分类模块的设计思路,网站备案承诺书怎么写,网站上做扫一扫,wordpress hotnews syntax errorPython 正则表达式模块 re
flyfish
一、正则表达式基础
1. 什么是正则表达式#xff1f;
正则表达式#xff08;Regular Expression, RE#xff09;是一种用于匹配、查找和替换文本模式的工具#xff0c;由普通字符#xff08;如字母、数字#xff09;和特殊字符
正则表达式Regular Expression, RE是一种用于匹配、查找和替换文本模式的工具由普通字符如字母、数字和特殊字符元字符组成。
2. 常用元字符
元字符说明示例.匹配任意单个字符除换行符a.c → abc, adc\w匹配字母、数字或下划线\w → hello123\d匹配数字\d{3} → 123\s匹配空白字符空格、制表符等\s → 多个空格*匹配前一个字符零次或多次ab* → a, ab, abb匹配前一个字符一次或多次ab → ab, abb?匹配前一个字符零次或一次ab? → a 或 ab^匹配字符串开头^abc → 以abc开头$匹配字符串结尾abc$ → 以abc结尾
二、Python 正则表达式模块 re
1. 模块导入
import re2. 常用函数
函数名作用描述re.compile()编译正则表达式提高重复使用效率re.match()从字符串开头匹配模式re.search()在字符串任意位置搜索模式re.findall()查找所有匹配项返回列表re.finditer()查找所有匹配项返回迭代器re.sub()替换匹配项re.subn()替换匹配项并返回替换次数re.split()按模式分割字符串re.fullmatch()要求整个字符串完全匹配模式
三、核心功能详解
1. 匹配操作
re.match()从开头匹配match re.match(rhello, hello world)
print(match.group()) # 输出: hellomatch re.search(rjson(.*?), content, re.DOTALL)
re.search() 函数
re.search(pattern, string, flags0) 是 re 模块中的一个函数用于在字符串 string 中搜索第一个与模式 pattern 匹配的子字符串。如果找到匹配项则返回一个匹配对象如果没有找到则返回 None。
pattern要搜索的正则表达式模式。string要在其中进行搜索的字符串这里是 content。flags可选参数用于指定正则表达式的匹配模式。这里使用了 re.DOTALL。
正则表达式模式 rjson(.*?)
r在字符串前面加上 r 表示这是一个原始字符串。在原始字符串中反斜杠 \ 不会被当作转义字符处理这样可以避免在编写正则表达式时出现过多的转义字符提高代码的可读性。json 这是一个普通的字符串表示匹配以 json 开头的文本。(.*?)这是一个捕获组用于匹配任意字符除换行符外除非使用了 re.DOTALL 标志。 .匹配除换行符外的任意单个字符。*表示前面的字符即 .可以出现零次或多次。?在 * 后面加上 ? 表示非贪婪匹配。贪婪匹配会尽可能多地匹配字符而非贪婪匹配会尽可能少地匹配字符。例如如果字符串中有多个 json...代码块非贪婪匹配会只匹配到第一个 就停止。 表示匹配以 结尾的文本。
re.DOTALL 标志
re.DOTALL 是 re 模块中的一个标志它会改变 . 的匹配行为。默认情况下. 不匹配换行符但使用 re.DOTALL 后. 可以匹配包括换行符在内的任意字符。这意味着代码块中可以包含换行符能够正确匹配多行的 JSON 代码块。
re.search()全局搜索search re.search(rworld, hello world)
print(search.group()) # 输出: world2. 查找所有匹配项
re.findall()numbers re.findall(r\d, a123b456c)
print(numbers) # 输出: [123, 456]3. 替换操作
re.sub()text re.sub(r\d, X, a123b456c)
print(text) # 输出: aXbXc4. 分割字符串
re.split()parts re.split(r\s, hello world)
print(parts) # 输出: [hello, world]四、捕获组与 group() 方法
1. 基本用法
pattern r(\d{4})-(\d{2})-(\d{2})
date_str 2025-03-11
match re.search(pattern, date_str)print(match.group(0)) # 完整匹配结果 → 2025-03-11
print(match.group(1)) # 第一个捕获组 → 2025
print(match.group(2)) # 第二个捕获组 → 03
print(match.group(3)) # 第三个捕获组 → 112. 查看捕获组数量
使用 groups()groups match.groups()
print(len(groups)) # 输出: 3命名捕获组使用 groupdict()pattern r(?Pyear\d{4})-(?Pmonth\d{2})-(?Pday\d{2})
match re.search(pattern, date_str)
print(match.groupdict()) # 输出: {year: 2025, month: 03, day: 11}五、re.match vs re.search
基本概念对比
re.match该函数会从字符串的起始位置开始尝试匹配正则表达式模式。如果字符串的起始位置不符合模式即使字符串的其他部分存在匹配内容re.match 也会返回 None。也就是说它要求模式必须从字符串的第一个字符开始匹配成功。re.search此函数会在整个字符串中进行搜索查找与正则表达式模式匹配的第一个位置。只要字符串中存在一处符合模式的内容re.search 就会返回一个匹配对象。
详细示例对比
示例 1模式在字符串起始位置匹配
import re# 定义字符串和模式
pattern rhello
string hello world# 使用 re.match
match_result re.match(pattern, string)
if match_result:print(re.match 匹配成功匹配内容为:, match_result.group())
else:print(re.match 匹配失败)# 使用 re.search
search_result re.search(pattern, string)
if search_result:print(re.search 匹配成功匹配内容为:, search_result.group())
else:print(re.search 匹配失败)结果分析在这个例子中模式 hello 位于字符串 hello world 的起始位置。因此re.match 和 re.search 都能成功匹配并且都能返回匹配到的 hello。
示例 2模式不在字符串起始位置
import re# 定义字符串和模式
pattern rworld
string hello world# 使用 re.match
match_result re.match(pattern, string)
if match_result:print(re.match 匹配成功匹配内容为:, match_result.group())
else:print(re.match 匹配失败)# 使用 re.search
search_result re.search(pattern, string)
if search_result:print(re.search 匹配成功匹配内容为:, search_result.group())
else:print(re.search 匹配失败)结果分析模式 world 不在字符串 hello world 的起始位置所以 re.match 会匹配失败返回 None。而 re.search 会在整个字符串中搜索能够找到 world 并返回匹配对象输出匹配内容 world。
示例 3模式部分在起始位置但不完全匹配
import re# 定义字符串和模式
pattern rhello world!
string hello world# 使用 re.match
match_result re.match(pattern, string)
if match_result:print(re.match 匹配成功匹配内容为:, match_result.group())
else:print(re.match 匹配失败)# 使用 re.search
search_result re.search(pattern, string)
if search_result:print(re.search 匹配成功匹配内容为:, search_result.group())
else:print(re.search 匹配失败)结果分析模式 hello world! 虽然前部分 hello world 与字符串起始部分相同但整体模式不完全匹配所以 re.match 会失败。re.search 同样在整个字符串中找不到完全匹配的内容也会匹配失败。
性能考虑
re.match由于它只从字符串起始位置开始匹配不需要对整个字符串进行遍历在某些情况下性能可能会更好特别是当你明确知道要匹配的内容应该在字符串开头时。re.search需要遍历整个字符串来查找匹配位置所以在处理较长字符串时性能可能会相对较低。但它的灵活性更高适用于不确定匹配内容位置的情况。
六、正则表达式 re 模块的常用例子
1. 匹配以特定字符开头的字符串
import retext apple banana cherry
pattern r^apple
result re.search(pattern, text)
if result:print(匹配成功:, result.group())
else:print(匹配失败)2. 匹配以特定字符结尾的字符串
import retext apple banana cherry
pattern rcherry$
result re.search(pattern, text)
if result:print(匹配成功:, result.group())
else:print(匹配失败)3. 匹配包含特定单词的字符串
import retext The quick brown fox jumps over the lazy dog
pattern rfox
result re.search(pattern, text)
if result:print(匹配成功:, result.group())
else:print(匹配失败)4. 匹配连续数字
import retext abc123def
pattern r\d
result re.findall(pattern, text)
print(匹配结果:, result)5. 匹配字母和数字的组合
import retext abc123def
pattern r[a-zA-Z0-9]
result re.findall(pattern, text)
print(匹配结果:, result)6. 匹配邮箱地址
import retext exampleexample.com
pattern r^[a-zA-Z0-9_.-][a-zA-Z0-9-]\.[a-zA-Z0-9-.]$
result re.fullmatch(pattern, text)
if result:print(匹配成功:, result.group())
else:print(匹配失败)7. 匹配手机号码
import retext 13800138000
pattern r^1[3-9]\d{9}$
result re.fullmatch(pattern, text)
if result:print(匹配成功:, result.group())
else:print(匹配失败)8. 匹配日期格式YYYY-MM-DD
import retext 2025-03-11
pattern r^\d{4}-\d{2}-\d{2}$
result re.fullmatch(pattern, text)
if result:print(匹配成功:, result.group())
else:print(匹配失败)9. 替换所有数字为指定字符
import retext abc123def456
pattern r\d
replacement X
result re.sub(pattern, replacement, text)
print(替换结果:, result)10. 分割字符串
import retext apple,banana,cherry
pattern r,
result re.split(pattern, text)
print(分割结果:, result)11. 提取 HTML 标签中的内容
import rehtml pHello, World!/p
pattern rp(.*?)/p
result re.findall(pattern, html)
print(提取结果:, result)12. 匹配中文
import retext 你好世界
pattern r[\u4e00-\u9fa5]
result re.findall(pattern, text)
print(匹配结果:, result)13. 匹配多个单词中的任意一个
import retext cat dog elephant
pattern rcat|dog
result re.findall(pattern, text)
print(匹配结果:, result)14. 匹配重复的字符
import retext aaaaabbbccc
pattern r(.)\1
result re.findall(pattern, text)
print(匹配结果:, result)15. 匹配不包含特定字符的字符串
import retext abcde
pattern r[^abc]
result re.findall(pattern, text)
print(匹配结果:, result)16. 匹配单词边界
import retext The quick brown fox jumps
pattern r\bfox\b
result re.search(pattern, text)
if result:print(匹配成功:, result.group())
else:print(匹配失败)17. 匹配 IP 地址
import retext 192.168.1.1
pattern r^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
result re.fullmatch(pattern, text)
if result:print(匹配成功:, result.group())
else:print(匹配失败)18. 匹配 URL
import retext https://www.example.com
pattern r^https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))
result re.fullmatch(pattern, text)
if result:print(匹配成功:, result.group())
else:print(匹配失败)19. 统计匹配次数
import retext apple apple banana cherry apple
pattern rapple
matches re.findall(pattern, text)
count len(matches)
print(匹配次数:, count)20. 使用编译后的正则表达式进行匹配
import retext abc123def
pattern re.compile(r\d)
result pattern.findall(text)
print(匹配结果:, result)