py学习笔记:正则表达式的使用

author = 'pengwei' # encoding: UTF-8 import re pattern = re.compile(r"d+") match = pattern.match('hello world!hello1212') if match: print(match.group()) else: print("不匹配") match = pattern.search('hello12 world!43') if match: print(match.group()) else: print("未找到") #分割字符串 print(pattern.split('one1two2three3four44a')) #匹配全部 print(pattern.findall('one1two2three3four467')) #搜索string,返回一个顺序访问每一个匹配结果(Match对象)的迭代器。 for m in pattern.finditer('one11two22three33four44'): print(m.group())

运行结果:

不匹配 12 ['one', 'two', 'three', 'four', 'a'] ['1', '2', '3', '467'] 11 22 33 44