import re # #1.search和match的区别 # pattern = re.compile(r'd+') # #match从头开始匹配 # m = pattern.match('onetwo12threefour34',6) # print(m) # #(在指定范围内查找 默认全局) 查找 只查一次 后面的8是从位置8开始只查一次 # s = pattern.search('onetwo12threefour34',8) # print(s)#34 # s = pattern.search('onetwo12threefour34',7) # print(s)#2 # print(s.group()) #2.分组 pattern = re.compile(r'(d+) (d+)') s = pattern.search('hello 123 456') print(s) print(type(s.group()))#str类型 print(s.group(1)) print(s.group(2)) print(s.groups())