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编程中一些重用与缩减的建议
Apr 14 Python
用PyQt进行Python图形界面的程序的开发的入门指引
Apr 14 Python
python算法演练_One Rule 算法(详解)
May 17 Python
HTML中使用python屏蔽一些基本功能的方法
Jul 07 Python
PyTorch学习笔记之回归实战
May 28 Python
python用户评论标签匹配的解决方法
May 31 Python
pytorch 数据集图片显示方法
Jul 26 Python
Python动态生成多维数组的方法示例
Aug 09 Python
python多进程控制学习小结
Oct 31 Python
Python线程之定位与销毁的实现
Feb 17 Python
jupyter notebook 恢复误删单元格或者历史代码的实现
Apr 17 Python
Tensorflow使用Anaconda、pycharm安装记录
Jul 29 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
DOTA2 无惧惊涛骇浪 昆卡大型水友攻略
2020/04/20 DOTA
在PHP中实现Javascript的escape()函数代码
2010/08/08 PHP
解析php session_set_save_handler 函数的用法(mysql)
2013/06/29 PHP
php中操作memcached缓存进行增删改查数据的实现代码
2014/08/15 PHP
php自定义加密与解密程序实例
2014/12/31 PHP
PHP中PDO事务处理操作示例
2018/05/02 PHP
jQuery AJAX 调用WebService实现代码
2010/03/24 Javascript
ASP.NET jQuery 实例9  通过控件hyperlink实现返回顶部效果
2012/02/03 Javascript
JavaScript中instanceof与typeof运算符的用法及区别详细解析
2013/11/19 Javascript
html文档中的location对象属性理解及常见的用法
2014/08/13 Javascript
ie8下修改input的type属性报错的解决方法
2014/09/16 Javascript
jQuery搜索同辈元素方法
2015/02/10 Javascript
深入分析Javascript跨域问题
2015/04/17 Javascript
jQuery实现单击弹出Div层窗口效果(可关闭可拖动)
2015/09/19 Javascript
JS实现适合于后台使用的动画折叠菜单效果
2015/09/21 Javascript
jquery文字填写自动高度的实现方法
2016/11/07 Javascript
微信小程序本作用域下调用全局JS详解及实例
2017/02/22 Javascript
微信小程序自定义导航隐藏和显示功能
2017/06/13 Javascript
p5.js入门教程之小球动画示例代码
2018/03/15 Javascript
新手简单了解vue
2019/05/29 Javascript
Vuex,iView UI面包屑导航使用扩展详解
2019/11/04 Javascript
python中dir函数用法分析
2015/04/17 Python
使用Python写CUDA程序的方法
2017/03/27 Python
Python实现iOS自动化打包详解步骤
2018/10/03 Python
解决安装python库时windows error5 报错的问题
2018/10/21 Python
Python 按字典dict的键排序,并取出相应的键值放于list中的实例
2019/02/12 Python
Python 进程之间共享数据(全局变量)的方法
2019/07/16 Python
django框架auth模块用法实例详解
2019/12/10 Python
Django用户身份验证完成示例代码
2020/04/03 Python
python和go语言的区别是什么
2020/07/20 Python
Python判断字符串是否为合法标示符操作
2020/09/03 Python
中间件分为哪几类
2016/09/18 面试题
2014年小学元旦活动方案
2014/02/12 职场文书
工作过失检讨书
2014/02/23 职场文书
《金孔雀轻轻跳》教学反思
2014/04/20 职场文书
银行贷款委托书范本
2014/10/11 职场文书