python正则表达式re模块详解


Posted in Python onJune 25, 2014

快速入门

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 相关文章推荐
Python提取网页中超链接的方法
Sep 18 Python
Python实现Pig Latin小游戏实例代码
Feb 02 Python
python实现监控某个服务 服务崩溃即发送邮件报告
Jun 21 Python
Python实用技巧之列表、字典、集合中根据条件筛选数据详解
Jul 11 Python
详解django中使用定时任务的方法
Sep 27 Python
Python通过for循环理解迭代器和生成器实例详解
Feb 16 Python
Python环境Pillow( PIL )图像处理工具使用解析
Sep 12 Python
pycharm解决关闭flask后依旧可以访问服务的问题
Apr 03 Python
python工具快速为音视频自动生成字幕(使用说明)
Jan 27 Python
手把手教你配置JupyterLab 环境的实现
Feb 02 Python
python中%格式表达式实例用法
Jun 18 Python
Pillow图像处理库安装及使用
Apr 12 Python
Python通过websocket与js客户端通信示例分析
Jun 25 #Python
Flask框架学习笔记(一)安装篇(windows安装与centos安装)
Jun 25 #Python
Python中文编码那些事
Jun 25 #Python
教你如何在Django 1.6中正确使用 Signal
Jun 22 #Python
python抓取网页时字符集转换问题处理方案分享
Jun 19 #Python
python在linux中输出带颜色的文字的方法
Jun 19 #Python
解决windows下Sublime Text 2 运行 PyQt 不显示的方法分享
Jun 18 #Python
You might like
PHP如何编写易读的代码
2007/07/10 PHP
细谈php中SQL注入攻击与XSS攻击
2012/06/10 PHP
教大家制作简单的php日历
2015/11/17 PHP
php把时间戳转换成多少时间之前函数的实例
2016/11/16 PHP
PHP PDOStatement::fetchAll讲解
2019/01/31 PHP
laravel 操作数据库常用函数的返回值方法
2019/10/11 PHP
html下载本地
2006/06/19 Javascript
extjs fckeditor集成代码
2009/05/10 Javascript
jQuery 位置函数offset,innerWidth,innerHeight,outerWidth,outerHeight,scrollTop,scrollLeft
2010/03/23 Javascript
纯JAVASCRIPT图表动画插件Highcharts Examples
2011/04/16 Javascript
jquery实现的带缩略图的焦点图片切换(自动播放/响应鼠标动作)
2013/01/23 Javascript
获取元素距离浏览器周边的位置的方法getBoundingClientRect
2013/04/17 Javascript
jquery对元素拖动排序示例
2014/01/16 Javascript
JavaScript从数组中删除指定值元素的方法
2015/03/18 Javascript
jQuery+canvas实现简单的球体斜抛及颜色动态变换效果
2016/01/28 Javascript
JS原型、原型链深入理解
2016/02/27 Javascript
JavaScript函数基础详解
2017/02/03 Javascript
jQuery中on方法使用注意事项详解
2017/02/15 Javascript
使用nodejs+express实现简单的文件上传功能
2017/12/27 NodeJs
nodejs express配置自签名https服务器的方法
2018/05/22 NodeJs
vue移动端屏幕适配详解
2019/04/30 Javascript
vue路由跳转传递参数的方式总结
2020/05/10 Javascript
写一个Vue loading 插件
2020/11/09 Javascript
[00:20]TI9观赛名额抽取Ⅱ
2019/07/24 DOTA
详解【python】str与json类型转换
2019/04/29 Python
python实现两个经纬度点之间的距离和方位角的方法
2019/07/05 Python
在python3中实现查找数组中最接近与某值的元素操作
2020/02/29 Python
Python多线程threading join和守护线程setDeamon原理详解
2020/03/18 Python
深入了解Python装饰器的高级用法
2020/08/13 Python
开业庆典邀请函
2014/01/08 职场文书
《小草和大树》教学反思
2014/02/16 职场文书
会计演讲稿范文
2014/05/23 职场文书
终止劳动合同协议书
2014/10/05 职场文书
神龙架导游词
2015/02/11 职场文书
Nginx反向代理多个服务器的实现方法
2021/03/31 Servers
python数据可视化使用pyfinance分析证券收益示例详解
2021/11/20 Python