手机
当前位置:查字典教程网 >脚本专栏 >python >python正则表达式re模块详解
python正则表达式re模块详解
摘要:快速入门importrepattern='this'text='Doesthistextmatchthepattern?'match...

快速入门

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"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 列表(List)操作方法详解

python 正则式使用心得

python正则表达式re模块详细介绍

python正则表达式判断字符串是否是全部小写示例

python 正则表达式 概述及常用字符

python 正则式 概述及常用字符

python 参数列表中的self 显式不等于冗余

python使用正则表达式检测密码强度源码分享

Python subprocess模块学习总结

Python ZipFile模块详解

精品推荐
分类导航