内置re模块。 re.match(表达式,字符串,修饰)尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。 re.search(表达式,字符串,修饰)扫描整个字符串并返回第一个成功的匹配。 re.match与re.search的区别 re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。 re.sub(表达式, 替换的字符串, 要被查找替换的字符串, 模式匹配后替换的最大次数,默认 0 表示替换所有的匹配, 修饰)
import re
str = 'hello word'
print(re.match(r'hello',str).span())
print(re.search(r'hello',str).span())
print(re.findall(r'o',str))