常见python正则用法的简单实例


Posted in Python onJune 21, 2016

下面列出Python正则表达式的几种匹配用法:

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 相关文章推荐
python文件比较示例分享
Jan 10 Python
python测试mysql写入性能完整实例
Jan 18 Python
python将一个英文语句以单词为单位逆序排放的方法
Dec 20 Python
Django实现学生管理系统
Feb 26 Python
Python实现的文轩网爬虫完整示例
May 16 Python
了解不常见但是实用的Python技巧
May 23 Python
python加载自定义词典实例
Dec 06 Python
解决torch.autograd.backward中的参数问题
Jan 07 Python
使用python将微信image下.dat文件解密为.png的方法
Nov 30 Python
python tqdm库的使用
Nov 30 Python
Python办公自动化之Excel(中)
May 24 Python
Python 类,对象,数据分类,函数参数传递详解
Sep 25 Python
小议Python中自定义函数的可变参数的使用及注意点
Jun 21 #Python
简单讲解Python编程中namedtuple类的用法
Jun 21 #Python
Python编程中实现迭代器的一些技巧小结
Jun 21 #Python
Centos Python2 升级到Python3的简单实现
Jun 21 #Python
Python的Django框架中forms表单类的使用方法详解
Jun 21 #Python
Python正则表达式使用经典实例
Jun 21 #Python
常见的python正则用法实例讲解
Jun 21 #Python
You might like
用PHP动态生成虚拟现实VRML网页
2006/10/09 PHP
PHP5新特性: 更加面向对象化的PHP
2006/11/18 PHP
一段防盗连的PHP代码
2006/12/06 PHP
PHP 导出数据到淘宝助手CSV的方法分享
2010/02/27 PHP
PHP文件管理之实现网盘及压缩包的功能操作
2017/09/20 PHP
PHP设计模式之建造者模式定义与用法简单示例
2018/08/13 PHP
JavaScript静态的动态
2006/09/18 Javascript
[转]JS宝典学习笔记
2007/02/07 Javascript
10个新的最有前途的JavaScript框架
2009/03/12 Javascript
jquery网页元素拖拽插件效果及实现
2013/08/05 Javascript
Nodejs Post请求报socket hang up错误的解决办法
2014/09/25 NodeJs
JavaScript中的Web worker多线程API研究
2014/12/06 Javascript
node.js中的fs.lchmodSync方法使用说明
2014/12/16 Javascript
PHP和NodeJs开发的应用如何共用Session
2015/04/16 NodeJs
jQuery+ajax实现无刷新级联菜单示例
2015/05/21 Javascript
Js的Array数组对象详解
2016/02/22 Javascript
解析javascript瀑布流原理实现图片滚动加载
2016/03/10 Javascript
JS如何设置cookie有效期为当天24点并弹出欢迎登陆界面
2016/08/04 Javascript
Jquery组件easyUi实现表单验证示例
2016/08/23 Javascript
javascript中apply/call和bind的使用
2017/02/15 Javascript
Js通过AES加密后PHP用Openssl解密的方法
2019/07/12 Javascript
layui table 多行删除(id获取)的方法
2019/09/12 Javascript
jQuery实现计算器功能
2020/10/19 jQuery
关于 Python opencv 使用中的 ValueError: too many values to unpack
2019/06/28 Python
无需JS和jQuery代码实现CSS3鼠标浮动放大图片
2016/11/21 HTML / CSS
纯CSS3实现运行时钟的示例代码
2021/01/25 HTML / CSS
您熟悉ORM(Object-Relation Mapping)吗?请谈谈您所理解的ORM
2016/02/08 面试题
大学生就业自荐信
2013/10/26 职场文书
教师岗位职责
2013/11/17 职场文书
初中英语教学反思
2014/01/25 职场文书
《雨点》教学反思
2014/02/12 职场文书
《三亚落日》教学反思
2014/04/26 职场文书
交通安全责任书范本
2014/07/24 职场文书
2015年管理人员工作总结
2015/05/13 职场文书
Nginx安装完成没有生成sbin目录的解决方法
2021/03/31 Servers
Win11软件图标固定到任务栏
2022/04/19 数码科技