
正则表达式Regular Expression简称 Regex是一种用于匹配、查找、替换文本中的字符串模式的工具。Python 通过内置的re模块支持正则表达式。一、常用功能功能说明模式Pattern用于描述匹配规则的字符串匹配Match检查字符串是否符合模式搜索Search在字符串中查找匹配的子串替换Replace将匹配的内容替换为其他字符串二、常用函数函数作用返回值re.match(pattern, string)从字符串开头匹配Match对象或Nonere.search(pattern, string)全文搜索第一个匹配Match对象或Nonere.findall(pattern, string)查找所有匹配返回列表列表re.finditer(pattern, string)查找所有匹配返回迭代器迭代器re.sub(pattern, repl, string)替换所有匹配新字符串re.split(pattern, string)按模式分割字符串列表re.compile(pattern)编译模式提高效率Pattern对象三、基础语法1. 字面字符import re # 直接匹配普通字符 pattern hello text hello world print(re.match(pattern, text)) # re.Match object print(re.match(hi, text)) # None2. 元字符元字符含义示例.匹配任意字符除换行符a.c匹配abc^匹配字符串开头^hello$匹配字符串结尾world$*匹配 0 次或多次a*匹配,a,aa匹配 1 次或多次a匹配a,aa?匹配 0 次或 1 次a?匹配,a{n}匹配正好 n 次a{3}匹配aaa{n,}匹配至少 n 次a{2,}匹配aa,aaa{n,m}匹配 n 到 m 次a{2,4}匹配aa,aaa,aaaa|或选择cat|dog匹配cat或dog()分组(ab)匹配ab,abab[]字符集[aeiou]匹配元音字母[^]否定字符集[^0-9]匹配非数字\转义\.匹配.3. 字符类字符类含义等价于\d数字[0-9]\D非数字[^0-9]\w单词字符字母、数字、下划线[a-zA-Z0-9_]\W非单词字符[^a-zA-Z0-9_]\s空白字符空格、制表符、换行[ \t\n\r\f\v]\S非空白字符[^ \t\n\r\f\v]\b单词边界\bword\b\B非单词边界四、常用匹配模式1. 匹配数字import re # 整数 re.match(r\d, 123abc) # 123 # 浮点数 re.search(r\d\.\d, pi3.14) # 3.14 # 负数 re.search(r-?\d, 温度-5度) # -52. 匹配邮箱email_pattern r[a-zA-Z0-9._%-][a-zA-Z0-9.-]\.[a-zA-Z]{2,} text 联系我: userexample.com print(re.search(email_pattern, text).group()) # userexample.com3. 匹配手机号中国phone_pattern r1[3-9]\d{9} text 我的电话是13812345678 print(re.search(phone_pattern, text).group()) # 13812345678五、分组与捕获1. 基本分组import re # 使用括号分组 pattern r(\d{4})-(\d{2})-(\d{2}) text 日期: 2024-01-15 match re.search(pattern, text) if match: print(match.group(0)) # 2024-01-15完整匹配 print(match.group(1)) # 2024第一组 print(match.group(2)) # 01第二组 print(match.group(3)) # 15第三组 print(match.groups()) # (2024, 01, 15)2. 命名分组pattern r(?Pyear\d{4})-(?Pmonth\d{2})-(?Pday\d{2}) text 日期: 2024-01-15 match re.search(pattern, text) if match: print(match.group(year)) # 2024 print(match.group(month)) # 01 print(match.group(day)) # 153. 非捕获分组# 使用 (?:...) 不捕获分组 pattern r(?:\d{4})-(?:\d{2})-(?:\d{2}) text 日期: 2024-01-15 # 匹配但不会保存分组六、贪婪与非贪婪匹配模式行为示例.*贪婪尽可能多匹配.*匹配整个字符串.*?非贪婪尽可能少匹配.*?匹配最小长度import re text h1标题/h1p段落/p # 贪婪匹配尽量多匹配 print(re.search(r.*, text).group()) # h1标题/h1p段落/p # 非贪婪匹配尽量少匹配 print(re.search(r.*?, text).group()) # h1七、替换与分割1. 替换import re text 我的电话是13812345678他的电话是13987654321 # 替换所有匹配 result re.sub(r\d{11}, ****, text) print(result) # 我的电话是****他的电话是**** # 使用回调函数替换 def hide_phone(match): return match.group()[:3] **** match.group()[-4:] result re.sub(r(\d{11}), hide_phone, text) print(result) # 我的电话是138****5678他的电话是139****43212. 分割import re text 苹果,香蕉;橘子 葡萄 # 按多个分隔符分割 result re.split(r[,; ], text) print(result) # [苹果, 香蕉, 橘子, 葡萄]八、编译正则表达式import re # 编译模式提高效率 pattern re.compile(r\d{3}-\d{4}-\d{4}) text 电话: 010-1234-5678 print(pattern.search(text).group()) # 010-1234-5678 # 预编译后可以反复使用 phones [010-1234-5678, 020-8765-4321, invalid] for phone in phones: if pattern.match(phone): print(f有效: {phone})常用函数匹配位置返回适用场景re.match()开头Match或None检查字符串开头re.search()全文Match或None查找第一个匹配re.findall()全文列表获取所有匹配re.finditer()全文迭代器遍历所有匹配re.sub()全文字符串替换所有匹配re.split()全文列表按模式分割