• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python正则表达式re模块详解

python正则表达式re模块详解

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了python re正则表达式,python正则表达式,python正则表达式教程,python正则表达式语法,python3 正则表达式等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

快速入门

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]))
</div>

执行结果:

#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))
</div>

执行结果:

#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))
</div>

执行结果:

#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))
</div>

执行结果:

#python re_finditer.py 
Found "ab" at 0:2
Found "ab" at 5:7
</div>

</div>

您可能想查找下面的文章:

  • Python的re模块正则表达式操作
  • Python基础教程之正则表达式基本语法以及re模块
  • python正则表达式re模块详解
  • PYTHON正则表达式 re模块使用说明
  • PYTHON正则表达式 re模块使用说明

相关文章

  • 把MySQL表结构映射为Python中的对象的教程
  • python sort、sorted高级排序技巧
  • Python获取apk文件URL地址实例
  • python操作日期和时间的方法
  • Python内置的HTTP协议服务器SimpleHTTPServer使用指南
  • python发布模块的步骤分享
  • Python脚本获取操作系统版本信息
  • Python使用Srapy框架爬虫模拟登陆并抓取知乎内容
  • Python的Flask框架中集成CKeditor富文本编辑器的教程
  • Python实现向QQ群成员自动发邮件的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python中动态获取对象的属性和方法的教程
    • python计算方程式根的方法
    • Python实现计算两个时间之间相差天数的方法
    • python 获取et和excel的版本号
    • Python中使用select模块实现非阻塞的IO
    • python中 ? : 三元表达式的使用介绍
    • 让python 3支持mysqldb的解决方法
    • 巧用Python装饰器 免去调用父类构造函数的麻烦
    • Python实现队列的方法
    • 详解Python的Django框架中的Cookie相关处理

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有