Python 正则表式
1.1 查找文本中的模式 1 import re 2 pattern = 'this' 3 text = 'Does this text match the pattern?' 4 match = re.search(pattern,text) 5 6 s = match.start() 7 e = match.end() 8 9 print(match.re.pattern,match.string,s,e,text[s:e]) 1.2 编译表达式 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import re 4 5 regexes = [re.compile(p) for p in ['this','that']] 6 text = 'Does this text match the pattern?' 7 print("Text: %r\n",text) 8 9 for regex in regexes: 10 print('Seeking "%s" ->' % regex.pattern) 11 if regex.search(text): 12 print('Match') 13 else: 14 print("No match!") 1.3 多重匹配 1 import re 2 text =