(...)分组
import re # (...)分组 string_01 = 'apple567WRAP899WRAP223funny2356' pattern_01 = '([A-Z]{4})[0-9]+' res_01 = re.search(pattern_01, string_01) print(res_01, res_01.group(), res_01.group(1)) # 打印结果: <re.Match object; span=(8, 15), match='WRAP899'> WRAP899 WRAP
(?P<name>)给分组起别名
import re # (?P<name>)给分组起别名 string_02 = 'apple567WRAP899WRAP223funny2356' pattern_02 = '(?P<name_01>[A-Z]{4})(?P<name_02>[0-9]+).' res_02 = re.search(pattern_02, string_02) print(res_02, res_02.groupdict()) print(res_02.group(), res_02.group(1), res_02.group(2)) # 按分组索引获取 print(res_02.group('name_01'), res_02.group('name_02')) # 按分组别名获取 # 打印结果: # <re.Match object; span=(8, 16), match='WRAP899W'> {'name_01': 'WRAP', 'name_02': '899'} # WRAP899W WRAP 899 # WRAP 899
(?P=name)引用分组
import re # (?P=name)引用分组 # 引用分组匹配到的内容需要和被引用分组匹配到的内容一模一样 string_03 = 'apple567WRAP899WRAP223funny2356' pattern_03 = '(?P<name_01>[A-Z]{4}).*(?P=name_01)' res_03 = re.search(pattern_03, string_03) print(res_03, res_03.groupdict()) print(res_03.group(), res_03.group(1)) # 引用分组无法被捕获,res_03.group(2)会报IndexError: no such group错 print(res_03.group('name_01')) # 打印结果: # <re.Match object; span=(8, 19), match='WRAP899WRAP'> {'name_01': 'WRAP'} # WRAP899WRAP WRAP # WRAP
(?#...) 正则中写注释括号里的内容会被忽略掉
import re string_04 = 'apple567WRAP899WRAP223funny2356' pattern_04 = '(?#这是注释)([A-Z]{4})[0-9]+' res_04 = re.search(pattern_04, string_04) print(res_04, res_04.group(), res_04.group(1)) # 注释分组无法被捕获 # 打印结果: <re.Match object; span=(8, 15), match='WRAP899'> WRAP899 WRAP
(?:...) 表示这个分组不会被捕获: 所以下面例子group(1)捕获到的是第二个分组
import re string_05 = 'apple567WRAP899WRAP223funny2356' pattern_05 = '(?:[A-Z]{4})([0-9]+)' res_05 = re.search(pattern_05, string_05) print(res_05, res_05.group(1)) # 打印结果: <re.Match object; span=(8, 15), match='WRAP899'> 899
(?=...) 后向界定 括号内的内容不会被匹配
import re # 括号中的 ... 代表你希望匹配的字符串后面应该出现的字符串 string_06 = 'apple567WRAP899WRAP223funny2356' pattern_06 = '[A-Z]{4}d{2}(?=3)' res_06 = re.search(pattern_06, string_06) print(res_06) # 打印结果: <re.Match object; span=(15, 21), match='WRAP22'>
(?!...) 后向非界定 和上面的后向界定相反,就相当于 not
import re string_07 = 'apple567WRAP899WRAP223funny2356' pattern_07 = '[A-Z]{4}d{2}(?!9)' res_07 = re.search(pattern_07, string_07) print(res_07) # 打印结果: <re.Match object; span=(15, 21), match='WRAP22'>
(?<=...) 前向界定 括号内的内容不会被匹配
import re # 括号中 ... 代表你希望匹配的字符串的前面应该出现的字符串 string_08 = 'apple567WRAP899WRAP223funny2356' pattern_08 = '(?<=99)[A-Z]{4}d' res_08 = re.search(pattern_08, string_08) print(res_08) # 打印结果: <re.Match object; span=(15, 20), match='WRAP2'>
(?<!...) 前向非界定 与上面前向界定相反
import re # 当希望的字符串前面不是 ... 的内容时才匹配 string_09 = 'apple567WRAP899WRAP223funny2356' pattern_09 = '(?<!567)[A-Z]{4}d' res_09 = re.search(pattern_09, string_09) print(res_09) # 打印结果: <re.Match object; span=(15, 20), match='WRAP2'>