Python正则表达式使用经典实例


Posted in Python onJune 21, 2016

下面列出Python正则表达式的几种匹配用法,具体内容如下所示:

此外,关于正则的一切http://deerchao.net/tutorials/regex/regex.htm

1.测试正则表达式是否匹配字符串的全部或部分

regex=ur"" #正则表达式
if re.search(regex, subject):
do_something()
else:
do_anotherthing()

2.测试正则表达式是否匹配整个字符串

regex=ur"\Z" #正则表达式末尾以\Z结束
if re.match(regex, subject):
    do_something()
else:
    do_anotherthing()

3.创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
    # match start: match.start()
    # match end (exclusive): atch.end()
    # matched text: match.group()
    do_something()
else:
    do_anotherthing()

4.获取正则表达式所匹配的子串(Get the part of a string matched by the regex)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
    result = match.group()
else:
    result = ""

5. 获取捕获组所匹配的子串(Get the part of a string matched by a capturing group)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
    result = match.group(1)
else:
    result = ""

6. 获取有名组所匹配的子串(Get the part of a string matched by a named group)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
result = match.group"groupname")
else:
result = ""

7. 将字符串中所有匹配的子串放入数组中(Get an array of all regex matches in a string)

result = re.findall(regex, subject)

8.遍历所有匹配的子串(Iterate over all matches in a string)

for match in re.finditer(r"<(.*?)\s*.*?/\1>", subject)
    # match start: match.start()
    # match end (exclusive): atch.end()
    # matched text: match.group()

9.通过正则表达式字符串创建一个正则表达式对象(Create an object to use the same regex for many operations)

reobj = re.compile(regex)

10.用法1的正则表达式对象版本(use regex object for if/else branch whether (part of) a string can be matched)

reobj = re.compile(regex)
if reobj.search(subject):
    do_something()
else:
    do_anotherthing()

11.用法2的正则表达式对象版本(use regex object for if/else branch whether a string can be matched entirely)

reobj = re.compile(r"\Z") #正则表达式末尾以\Z 结束
if reobj.match(subject):
    do_something()
else:
    do_anotherthing()

12.创建一个正则表达式对象,然后通过该对象获得匹配细节(Create an object with details about how the regex object matches (part of) a string)

reobj = re.compile(regex)
match = reobj.search(subject)
if match:
    # match start: match.start()
    # match end (exclusive): atch.end()
    # matched text: match.group()
    do_something()
else:
    do_anotherthing()

13.用正则表达式对象获取匹配子串(Use regex object to get the part of a string matched by the regex)

reobj = re.compile(regex)
match = reobj.search(subject)
if match:
    result = match.group()
else:
    result = ""

14.用正则表达式对象获取捕获组所匹配的子串(Use regex object to get the part of a string matched by a capturing group)

reobj = re.compile(regex)
match = reobj.search(subject)
if match:
    result = match.group(1)
else:
    result = ""

15.用正则表达式对象获取有名组所匹配的子串(Use regex object to get the part of a string matched by a named group)

reobj = re.compile(regex)
match = reobj.search(subject)
if match:
    result = match.group("groupname")
else:
    result = ""

16.用正则表达式对象获取所有匹配子串并放入数组(Use regex object to get an array of all regex matches in a string)

reobj = re.compile(regex)
result = reobj.findall(subject)

17.通过正则表达式对象遍历所有匹配子串(Use regex object to iterate over all matches in a string)

reobj = re.compile(regex)
for match in reobj.finditer(subject):
    # match start: match.start()
    # match end (exclusive): match.end()
    # matched text: match.group()

字符串替换

1.替换所有匹配的子串

#用newstring替换subject中所有与正则表达式regex匹配的子串
result = re.sub(regex, newstring, subject)

2.替换所有匹配的子串(使用正则表达式对象)

reobj = re.compile(regex)
result = reobj.sub(newstring, subject)

字符串拆分

1.字符串拆分

result = re.split(regex, subject)

2.字符串拆分(使用正则表示式对象)

reobj = re.compile(regex)
result = reobj.split(subject)
Python 相关文章推荐
Python 变量类型及命名规则介绍
Jun 08 Python
python文件和目录操作方法大全(含实例)
Mar 12 Python
使用python遍历指定城市的一周气温
Mar 31 Python
Python 中Pickle库的使用详解
Feb 24 Python
解决pycharm无法调用pip安装的包问题
May 18 Python
对Python3之方法的覆盖与super函数详解
Jun 26 Python
python对csv文件追加写入列的方法
Aug 01 Python
Pandas聚合运算和分组运算的实现示例
Oct 17 Python
Python 依赖库太多了该如何管理
Nov 08 Python
Python通过文本和图片生成词云图
May 21 Python
tensorflow图像裁剪进行数据增强操作
Jun 30 Python
Python 的 sum() Pythonic 的求和方法详细
Oct 16 Python
常见的python正则用法实例讲解
Jun 21 #Python
实例讲解Python的函数闭包使用中应注意的问题
Jun 20 #Python
Python中的数学运算操作符使用进阶
Jun 20 #Python
Python中在for循环中嵌套使用if和else语句的技巧
Jun 20 #Python
解析Python中的生成器及其与迭代器的差异
Jun 20 #Python
Python判断列表是否已排序的各种方法及其性能分析
Jun 20 #Python
Python编程中装饰器的使用示例解析
Jun 20 #Python
You might like
php中static静态变量的使用方法详解
2010/06/04 PHP
php中解析带中文字符的url函数分享
2015/01/20 PHP
php实现基于pdo的事务处理方法示例
2017/07/21 PHP
深入理解 PHP7 中全新的 zval 容器和引用计数机制
2018/10/15 PHP
PHP5.6读写excel表格文件操作示例
2019/02/26 PHP
js+CSS 图片等比缩小并垂直居中实现代码
2008/12/01 Javascript
JS连连看源码完美注释版(推荐)
2013/12/09 Javascript
node.js中的fs.fstatSync方法使用说明
2014/12/15 Javascript
javascript实现消灭星星小游戏简单版
2016/11/15 Javascript
Angular2利用组件与指令实现图片轮播组件
2017/03/27 Javascript
HTML的select控件美化
2017/03/27 Javascript
JS禁止浏览器右键查看元素或按F12审查元素自动关闭页面示例代码
2017/09/07 Javascript
vue中添加mp3音频文件的方法
2018/03/02 Javascript
详解Vue 多级组件透传新方法provide/inject
2018/05/09 Javascript
JS判断用户用的哪个浏览器实例详解
2018/10/09 Javascript
在Python程序中实现分布式进程的教程
2015/04/28 Python
在Python中实现替换字符串中的子串的示例
2018/10/31 Python
python re.sub()替换正则的匹配内容方法
2019/07/22 Python
Python 如何提高元组的可读性
2019/08/26 Python
Flask框架 CSRF 保护实现方法详解
2019/10/30 Python
python numpy实现多次循环读取文件 等间隔过滤数据示例
2020/03/14 Python
Python猫眼电影最近上映的电影票房信息
2020/09/18 Python
详解如何在pyqt中通过OpenCV实现对窗口的透视变换
2020/09/20 Python
详解向scrapy中的spider传递参数的几种方法(2种)
2020/09/28 Python
python实现银行账户系统
2021/02/22 Python
德国最大的网上鞋店之一:Schuhe24.de
2017/06/10 全球购物
美国购买当代和现代家具网站:MODTEMPO
2018/07/20 全球购物
金鑫耀Java笔试题
2014/09/06 面试题
普通大学毕业生自荐信
2013/11/04 职场文书
《彩色世界》教学反思
2014/04/12 职场文书
党员创先争优活动总结
2014/05/04 职场文书
物业工程部岗位职责
2015/02/11 职场文书
三下乡个人总结
2015/03/04 职场文书
廉洁自律证明
2015/06/24 职场文书
运动会跳远广播稿
2015/08/19 职场文书
世界各国短波电台对东亚播送时间频率表(SW)
2021/06/28 无线电