python正则表达式re模块详解
2014-06-25 11:04
1131 查看
快速入门
import re pattern = 'this' text = 'Does this text match the pattern?' match = re.search(pattern, text) s = match.start() e = match.end() print('Found "{0}"\nin "{1}"'.format(match.re.pattern, match.string)) print('from {0} to {1} ("{2}")'.format( s, e, text[s:e]))
执行结果:
#python re_simple_match.py Found "this" in "Does this text match the pattern?" from 5 to 9 ("this") import re # Precompile the patterns regexes = [ re.compile(p) for p in ('this', 'that')] text = 'Does this text match the pattern?' print('Text: {0}\n'.format(text)) for regex in regexes: if regex.search(text): result = 'match!' else: result = 'no match!' print('Seeking "{0}" -> {1}'.format(regex.pattern, result))
执行结果:
#python re_simple_compiled.py Text: Does this text match the pattern? Seeking "this" -> match! Seeking "that" -> no match! import re text = 'abbaaabbbbaaaaa' pattern = 'ab' for match in re.findall(pattern, text): print('Found "{0}"'.format(match))
执行结果:
#python re_findall.py Found "ab" Found "ab" import re text = 'abbaaabbbbaaaaa' pattern = 'ab' for match in re.finditer(pattern, text): s = match.start() e = match.end() print('Found "{0}" at {1}:{2}'.format(text[s:e], s, e))
执行结果:
#python re_finditer.py Found "ab" at 0:2 Found "ab" at 5:7
您可能感兴趣的文章:
相关文章推荐
- Python中re(正则表达式)模块详解
- python 详解正则表达式的使用(re模块)
- Python中re(正则表达式)模块详解
- python模块之re正则表达式详解
- python正则表达式re模块用法详解
- python与正则表达式:re模块详解
- python正则表达式:re模块
- Python中re(正则表达式)模块学习
- Python模块学习 re 正则表达式
- python re模块实现正则表达式
- Python的re模块(正则表达式操作)
- Python之re模块 —— 正则表达式操作
- 转 python内置正则表达式(re)模块官方文档简要中文版
- Python模块学习 ---- re 正则表达式
- Python笔记(8)re模块,正则表达式
- Python中re(正则表达式)模块函数学习
- python中正则表达式与re 模块
- Python用re(正则表达式)模块过滤Telnet字体颜色
- python的正则表达式re模块的常用方法
- Python模块学习 re 正则表达式