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 相关文章推荐
python3.0 字典key排序
Dec 24 Python
Python使用zip合并相邻列表项的方法示例
Mar 17 Python
pandas 小数位数 精度的处理方法
Jun 09 Python
pygame游戏之旅 添加游戏介绍
Nov 20 Python
Python实现决策树并且使用Graphviz可视化的例子
Aug 09 Python
python应用文件读取与登录注册功能
Sep 23 Python
python 链接sqlserver 写接口实例
Mar 11 Python
Python操作Excel把数据分给sheet
May 20 Python
Python列表如何更新值
May 27 Python
python编写softmax函数、交叉熵函数实例
Jun 11 Python
Python lambda表达式原理及用法解析
Aug 18 Python
pycharm 的Structure界面设置操作
Feb 05 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
不用mod_rewrite直接用php实现伪静态化页面代码
2008/10/04 PHP
php侧拉菜单 漂亮,可以向右或者向左展开,支持FF,IE
2009/10/15 PHP
PHP文件操作实例总结
2016/09/27 PHP
浅谈laravel中的关联查询with的问题
2019/10/10 PHP
jquery.boxy插件的iframe扩展代码
2010/07/02 Javascript
JS之Date对象和获取系统当前时间详解
2014/01/13 Javascript
javascript移出节点removeChild()使用介绍
2014/04/03 Javascript
js加密解密字符串可自定义密码因子
2014/05/13 Javascript
NodeJS使用jQuery选择器操作DOM
2015/02/13 NodeJs
js制作带有遮罩弹出层实现登录注册表单特效代码分享
2015/09/05 Javascript
jquery+json实现数据二级联动的方法
2015/11/28 Javascript
基于javascript实现右下角浮动广告效果
2016/01/08 Javascript
JavaScript学习笔记之ES6数组方法
2016/03/25 Javascript
针对BootStrap中tabs控件的美化和完善(推荐)
2016/07/06 Javascript
JS遍历页面所有对象属性及实现方法
2016/08/01 Javascript
jQuery插件实现可输入和自动匹配的下拉框
2016/10/24 Javascript
AngularJS select设置默认值的实现方法
2017/08/25 Javascript
详解Koa中更方便简单发送响应的方式
2018/07/20 Javascript
javascript数据结构之多叉树经典操作示例【创建、添加、遍历、移除等】
2018/08/01 Javascript
使用Javascript简单计算器
2018/11/17 Javascript
Vue表单之v-model绑定下拉列表功能
2019/05/14 Javascript
vue实现单一筛选、删除筛选条件
2020/10/26 Javascript
[01:02:26]DOTA2-DPC中国联赛 正赛 SAG vs RNG BO3 第二场 1月18日
2021/03/11 DOTA
详解Python异常处理中的Finally else的功能
2017/12/29 Python
Python根据已知邻接矩阵绘制无向图操作示例
2018/06/23 Python
Python Cookie 读取和保存方法
2018/12/28 Python
Python3如何对urllib和urllib2进行重构
2019/11/25 Python
Matplotlib.pyplot 三维绘图的实现示例
2020/07/28 Python
阻止移动设备(手机、pad)浏览器双击放大网页的方法
2014/06/03 HTML / CSS
一些Unix笔试题和面试题
2013/01/22 面试题
接受捐赠答谢词
2014/01/27 职场文书
优秀信贷员先进事迹
2014/01/31 职场文书
新闻专业毕业生英文求职信
2014/03/19 职场文书
财产公证书
2014/04/10 职场文书
2015年“世界无车日”活动方案
2015/05/06 职场文书
JS前端监控采集用户行为的N种姿势
2022/07/23 Javascript