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 24 Python
浅谈Python由__dict__和dir()引发的一些思考
Oct 30 Python
Python数据结构与算法之列表(链表,linked list)简单实现
Oct 30 Python
JS设计模式之责任链模式实例详解
Feb 03 Python
python线程中同步锁详解
Apr 27 Python
浅谈tensorflow中几个随机函数的用法
Jul 27 Python
Python控制键盘鼠标pynput的详细用法
Jan 28 Python
django框架防止XSS注入的方法分析
Jun 21 Python
Python使用OpenPyXL处理Excel表格
Jul 02 Python
python入门教程之基本算术运算符
Nov 13 Python
Python 远程开关机的方法
Nov 18 Python
Python的Tqdm模块实现进度条配置
Feb 24 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 Mysql类 可以参考学习熟悉下
2009/06/21 PHP
php调用KyotoTycoon简单实例
2015/04/02 PHP
IE和Mozilla的兼容性汇总event
2007/08/12 Javascript
javascript制作的简单注册模块表单验证
2015/04/13 Javascript
JS实现自动定时切换的简洁网页选项卡效果
2015/10/13 Javascript
jquery中cookie用法实例详解(获取,存储,删除等)
2016/01/04 Javascript
详解javascript事件绑定使用方法
2016/10/20 Javascript
详解webpack 配合babel 将es6转成es5 超简单实例
2017/05/02 Javascript
Nodejs下使用gm圆形裁剪并合成图片的示例
2018/02/22 NodeJs
angularjs数组判断是否含有某个元素的实例
2018/02/27 Javascript
vue 的keep-alive缓存功能的实现
2018/03/22 Javascript
自定义Vue组件打包、发布到npm及使用教程
2019/05/22 Javascript
原生js实现二级联动菜单
2019/11/27 Javascript
Jquery Datatables的使用详解
2020/01/30 jQuery
Python中使用ConfigParser解析ini配置文件实例
2014/08/30 Python
Python的Django框架中TEMPLATES项的设置教程
2015/05/29 Python
在Lighttpd服务器中运行Django应用的方法
2015/07/22 Python
Python实现定时自动关闭的tkinter窗口方法
2019/02/16 Python
Python中字符串List按照长度排序
2019/07/01 Python
python实现ssh及sftp功能(实例代码)
2020/03/16 Python
python中查看.db文件中表格的名字及表格中的字段操作
2020/07/07 Python
HTML5 Canvas中使用用路径描画圆弧
2015/01/01 HTML / CSS
Weekendesk意大利:探索多种引人入胜的周末主题
2016/10/14 全球购物
全球最大的房车租赁市场:Outdoorsy
2018/09/19 全球购物
Android面试宝典
2013/08/06 面试题
葡萄牙语专业个人求职信
2013/12/10 职场文书
英语系本科生求职信范文
2013/12/18 职场文书
一名老师的自我评价
2014/02/07 职场文书
《莫高窟》教学反思
2014/02/25 职场文书
大学优秀班集体申报材料
2014/05/23 职场文书
五好家庭事迹材料
2014/12/20 职场文书
考试作弊检讨
2015/01/27 职场文书
写自招自荐信的绝招!
2019/04/19 职场文书
个人房屋租赁合同(标准范本)
2019/09/16 职场文书
字典算法实现及操作 --python(实用)
2021/03/31 Python
浅谈pytorch中的dropout的概率p
2021/05/27 Python