一、findall 、search 使用方法同上一章
♦ match('a', 'abc').group() 同search,不过只在字符串开始处匹配
re.match("d+", "56sdfcvsdf38").group() '56'
♦ split:分割
re.split(" ", "hello worlld") ['hello', 'worlld'] re.split("[ab]", "asdabcd") #以a或b分割,先以a分,a左边无内容,返回空字符串 ['', 'sd', '', 'cd'] re.split("[ab]", "asdbcd") ['', 'sd', 'cd']
♦ sub :替换
re.sub("d+", "X", "asd123sdff34") 'asdXsdffX' re.sub("d", "X", "asd123sdff34") 'asdXXXsdffXX'
re.sub("d", "X", "asd123sdff34", 3) #指定替换次数 'asdXXXsdff34'
re.subn("d", "X", "asd123sdff34") #查看替换次数 ('asdXXXsdffXX', 5)
♦ finditer :作用跟findall差不多,返回的是一个迭代器对象
ret = re.finditer("d","asd123sdff34") next(ret).group() '1' ret.__next__().group() '2'