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中while循环语句用法简单实例
May 07 Python
Python实现Linux命令xxd -i功能
Mar 06 Python
浅谈python对象数据的读写权限
Sep 12 Python
python通过pip更新所有已安装的包实现方法
May 19 Python
Python学习教程之常用的内置函数大全
Jul 14 Python
python实现决策树、随机森林的简单原理
Mar 26 Python
python3获取两个日期之间所有日期,以及比较大小的实例
Apr 08 Python
对python多线程与global变量详解
Nov 09 Python
python多线程使用方法实例详解
Dec 30 Python
解决tensorboard多个events文件显示紊乱的问题
Feb 15 Python
python空元组在all中返回结果详解
Dec 15 Python
python 爬取哔哩哔哩up主信息和投稿视频
Jun 07 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
对盗链说再见...
2006/10/09 PHP
PHP 采集程序原理分析篇
2010/03/05 PHP
《PHP编程最快明白》第七讲:php图片验证码与缩略图
2010/11/01 PHP
yii2中关于加密解密的那些事儿
2018/06/12 PHP
Yii2框架操作数据库的方法分析【以mysql为例】
2019/05/27 PHP
laravel实现上传图片,并且制作缩略图,按照日期存放的代码
2019/10/16 PHP
javascript IFrame 强制刷新代码
2009/07/23 Javascript
五段实用的js高级技巧
2011/12/20 Javascript
JS特殊函数(Function()构造函数、函数直接量)区别介绍
2013/05/19 Javascript
js浏览器本地存储store.js介绍及应用
2014/05/13 Javascript
javascript模拟实现ajax加载框实例
2014/10/15 Javascript
JavaScript中Window对象的属性及事件
2015/12/25 Javascript
JS、jQuery中select的用法详解
2016/04/21 Javascript
详解操作虚拟dom模拟react视图渲染
2018/07/25 Javascript
[06:20]2015国际邀请赛第三日top10
2015/08/08 DOTA
Python实现去除代码前行号的方法
2015/03/10 Python
TensorFlow模型保存和提取的方法
2018/03/08 Python
python如何统计序列中元素
2020/07/31 Python
Python调用jar包方法实现过程解析
2020/08/11 Python
CSS3 创建网页动画实现弹跳球动效果
2018/10/30 HTML / CSS
英国著名的茶叶品牌:Whittard of Chelsea
2016/09/22 全球购物
英国计算机产品零售商:Novatech(定制个人电脑、笔记本电脑、工作站和服务器)
2018/01/28 全球购物
美国电子产品主要品牌的授权在线零售商:DataVision
2019/03/23 全球购物
波兰家居饰品和厨房配件网上商店:Maleomi
2020/12/15 全球购物
建筑文秘专业个人求职信范文
2013/12/28 职场文书
评析教师个人的自我评价
2014/02/19 职场文书
中文教师求职信
2014/02/22 职场文书
《动手做做看》教学反思
2014/04/09 职场文书
领导班子个人对照检查剖析材料
2014/09/29 职场文书
员工表扬信怎么写
2015/05/05 职场文书
创业计划书详解
2019/07/19 职场文书
MySQL8.0的WITH查询详情
2021/08/30 MySQL
Java十分钟精通进阶适配器模式
2022/04/06 Java/Android
动画《朋友游戏》公开佐藤友生绘制的开播纪念绘
2022/04/06 日漫
《勇者辞职不干了》ED主题曲无字幕动画MV公开
2022/04/13 日漫
Python Django / Flask如何使用Elasticsearch
2022/04/19 Python