Python正则表达式常用函数总结


Posted in Python onJune 24, 2017

本文实例总结了Python正则表达式常用函数。分享给大家供大家参考,具体如下:

re.match()

函数原型:

match(pattern, string, flags=0)
    Try to apply the pattern at the start of the string,
     returning a match object, or None if no match was found.

函数作用:

re.match函数尝试从字符串的开头开始匹配一个模式,如果匹配成功,返回一个匹配成功的对象,否则返回None。

参数说明:

pattern:匹配的正则表达式
string:要匹配的字符串
flags:标志位,用于控制正则表达式的匹配方式。如是否区分大小写、是否多行匹配等。

我们可以使用group()或groups()匹配对象函数来获取匹配后的结果。

group()

group(...)
    group([group1, ...]) -> str or tuple.
    Return subgroup(s) of the match by indices or names.
    For 0 returns the entire match.

获得一个或多个分组截获的字符串;指定多个参数时将以元组形式返回。group1可以使用编号也可以使用别名;编号0代表匹配的整个子串;默认返回group(0);没有截获字符串的组返回None;截获了多次的组返回最后一次截获的子串。

groups()

groups(...)
    groups([default=None]) -> tuple.
    Return a tuple containing all the subgroups of the match, from 1.
    The default argument is used for groups
    that did not participate in the match

以元组形式返回全部分组截获的字符串。相当于调用group(1,2,…last)。没有截获字符串的组以默认值None代替。

实例

import re
line = "This is the last one"
res = re.match( r'(.*) is (.*?) .*', line, re.M|re.I)
if res:
 print "res.group() : ", res.group()
 print "res.group(1) : ", res.group(1)
 print "res.group(2) : ", res.group(2)
 print "res.groups() : ", res.groups()
else:
 print "No match!!"

re.M|re.I:这两参数表示多行匹配|不区分大小写,同时生效。

细节实例:

>>> re.match(r'.*','.*g3jl\nok').group()
'.*g3jl'

.(点)表示除换行符以外的任意一个字符,*(星号)表示匹配前面一个字符0次1次或多次,这两联合起来使用表示匹配除换行符意外的任意多个字符,所以出现以上的结果。

1、
re.match(r'.*..', '..').group()
'..'
2、
>>> re.match(r'.*g.','.*g3jlok').group()
'.*g3'
3、
>>> re.match(r'.*...', '..').group()
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'

上面两例子为什么有结果呢?这是因为第一个例子.*..中的.*匹配了0次,后面的..匹配字符串中..,而第二个例子中的 .* 匹配了一次,匹配字符串中的 .*,g匹配了后面的g字符,最后一个.号匹配了。
为什么第三个例子没有匹配到结果呢?这是因为就算正则表达式中的 .* 匹配0次,后面的三个点也不能完全匹配原字符串中的两个点,所以匹配失败了。
从上面几个例子可以看出,只有当正则表达式中要匹配的字符数小于等于原字符串中的字符数,才能匹配出结果。并且 “.*” 在匹配的过程中会回溯,先匹配0次,如果整个表达式能匹配成功,再匹配一次,如果还是能匹配,那就匹配两次,这样一次下去,直到不能匹配成功时,返回最近一次匹配成功的结果,这就是”.*”的贪婪性。

匹配Python中的标识符:

>>> re.match(r'^[a-zA-Z|_][\w_]*','_1name1').group()
'_1name1'
>>> re.match(r'^[a-zA-Z|_][\w_]*','_name1').group()
'_name1'
>>> re.match(r'^[a-zA-Z|_][\w_]*','num').group()
'num'
>>> re.match(r'^[a-zA-Z|_][\w_]*','1num').group()
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'

re.search()

函数原型:

search(pattern, string, flags=0)
    Scan through string looking for a match to the pattern,
    returning a match object, or None if no match was found.

函数作用:

扫描整个字符串并返回第一次成功的匹配对象,如果匹配失败,则返回None。

参数说明:

pattern:匹配的正则表达式
string:要匹配的字符串
flags:标志位,用于控制正则表达式的匹配方式。如是否区分大小写、是否多行匹配等。

跟re.match函数一样,使用group()和groups()方法来获取匹配后的结果。

>>> re.search(r'[abc]\*\d{2}','12a*23Gb*12ad').group()
'a*23'

从匹配结果看出,re.search返回了第一次匹配成功的结果'a*23',如果尽可能多的匹配的话,还可以匹配后面的'b*12'。

re.match与re.search的区别

re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配,否则也返回None。

>>> re.match(r'(.*)(are)',"Cats are smarter than dogs").group(2)
'are'
>>> re.search(r'(are)+',"Cats are smarter than dogs").group()
'are'

上面两个例子是等价的。

re.sub()

Python的re模块中提供了re.sub()函数用于替换字符串中的匹配项,如果没有匹配的项则字符串将没有匹配的返回。

函数原型:

sub(pattern, repl, string, count=0, flags=0)
    Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl.  repl can be either a string or a callable;
    if a string, backslash escapes in it are processed.  If it is
    a callable, it's passed the match object and must return
    a replacement string to be used.

参数说明:

pattern:匹配的正则表达式
repl:用于替换的字符串
string:要被替换的字符串
count:替换的次数,如果为0表示替换所有匹配到的字串,如果是1表示替换1次等,该参数必须是非负整数,默认为0。
flags:标志位,用于控制正则表达式的匹配方式。如是否区分大小写、是否多行匹配等。

实例

将手机号的后4位替换成0

>>> re.sub('\d{4}$','0000','13549876489')
'13549870000'

将代码后面的注释信息去掉

>>> re.sub('#.*$','', 'num = 0 #a number')
'num = 0 '

re.split()

函数原型:

split(pattern, string, maxsplit=0, flags=0)
    Split the source string by the occurrences of the pattern,
    returning a list containing the resulting substrings.

函数作用:

分割字符串,将字符串用给定的正则表达式匹配的字符串进行分割,分割后返回结果list。

参数说明:

pattern:匹配的正则表达式
string:被分割的字符串
maxsplit:最大的分割次数
flags:标志位,用于控制正则表达式的匹配方式。如是否区分大小写、是否多行匹配等。

re.findall()

函数原型:

findall(pattern, string, flags=0)
    Return a list of all non-overlapping matches in the string.
    If one or more groups are present in the pattern, return a
    list of groups; this will be a list of tuples if the pattern
    has more than one group.
    Empty matches are included in the result.

函数的作用:

获取字符串中所有匹配的字符串,并以列表的形式返回。列表中的元素有如下几种情况:

当正则表达式中含有多个圆括号()时,列表的元素为多个字符串组成的元组,而且元组中字符串个数与括号对数相同,并且字符串排放顺序跟括号出现的顺序一致(一般看左括号'(‘就行),字符串内容与每个括号内的正则表达式想对应。
当正则表达式中只带有一个圆括号时,列表中的元素为字符串,并且该字符串的内容与括号中的正则表达式相对应。(注意:列表中的字符串只是圆括号中的内容,不是整个正则表达式所匹配的内容。)
当正则表达式中没有圆括号时,列表中的字符串表示整个正则表达式匹配的内容。

参数说明:

pattern:匹配的正则表达式
string:被分割的字符串
flags:标志位,用于控制正则表达式的匹配方式。如是否区分大小写、是否多行匹配等。

实例:

1、匹配字符串中所有含有'oo'字符的单词

#正则表达式中没有括号
>>> re.findall(r'\w*oo\w*', 'woo this foo is too')
['woo', 'foo', 'too']

从结果可以看出,当正则表达式中没有圆括号时,列表中的字符串表示整个正则表达式匹配的内容

2、获取字符串中所有的数字字符串

#正则表达式中只有1个括号
>>> re.findall(r'.*?(\d+).*?','adsd12343.jl34d5645fd789')
['12343', '34', '5645', '789']

从上面结果可以看出,当正则表达式中只带有一个圆括号时,列表中的元素为字符串,并且该字符串的内容与括号中的正则表达式相对应。

3、提取字符串中所有的有效的域名地址

#正则表达式中有多个括号时
>>> add = 'https://www.net.com.edu//action=?asdfsd and other https://www.baidu.com//a=b'
>>> re.findall(r'((w{3}\.)(\w+\.)+(com|edu|cn|net))',add)
[('www.net.com.edu', 'www.', 'com.', 'edu'), ('www.baidu.com', 'www.', 'baidu.','com')]

从执行结果可以看出,正则表达式中有多个圆括号时,返回匹配成功的列表中的每一个元素都是由一次匹配成功后,正则表达式中所有括号中匹配的内容组成的元组。

re.finditer()

函数原型:

finditer(pattern, string, flags=0)
    Return an iterator over all non-overlapping matches in the string.  For each match, the iterator
returns a match object.
    Empty matches are included in the result.

函数作用:

跟re.findall()函数一样,匹配字符串中所有满足的字串,只是返回的是一个迭代器,而不是一个像findall函数那样存有所有结果的list,这个迭代器里面存的是每一个结果的一个匹配对象,这样可以节省空间,一般用在需要匹配大量的结果时,类似于range和xrange的区别。

参数说明:

pattern:匹配的正则表达式
string:被分割的字符串
flags:标志位,用于控制正则表达式的匹配方式。如是否区分大小写、是否多行匹配等。

如:匹配字符串中所有的数字字串

>>> for i in re.finditer(r'\d+','one12two34three56four') :
...  print i.group(),
...
12 34 56

start()

返回匹配的起始位置。如:

>>> re.search(r'\d+', 'asdf13df234').start()

注意,索引位置是从0开始计数的。

end()

返回匹配结束的下一个位置。如:

>>> re.search(r'\d+', 'asdf13df234').end()

span()

返回匹配的区间,左闭右开。如:

>>> re.search(r'\d+', 'asdf13df234').span()
(4, 6)

re.compile()

函数原型:

compile(pattern, flags=0)
    Compile a regular expression pattern, returning a pattern object.

函数作用:

编译一个正则表达式语句,并返回编译后的正则表达式对象。
这样我们就可以将那些经常使用的正则表达式编译成正则表达式对象,可以提高一定的效率。如:
一句话包含五个英文单词,长度不一定,用空格分割,请把五个单词匹配出来

>>> s = "this is  a python test"
>>> p = re.compile('\w+') #编译正则表达式,获得其对象
>>> res = p.findall(s)#用正则表达式对象去匹配内容
>>> print res
['this', 'is', 'a', 'python', 'test']
Python 相关文章推荐
Python实现文件复制删除
Apr 19 Python
详解Python编程中对Monkey Patch猴子补丁开发方式的运用
May 27 Python
python+matplotlib实现礼盒柱状图实例代码
Jan 16 Python
取numpy数组的某几行某几列方法
Apr 03 Python
Python3获取拉勾网招聘信息的方法实例
Apr 03 Python
Python+opencv 实现图片文字的分割的方法示例
Jul 04 Python
Django关于admin的使用技巧和知识点
Feb 10 Python
tensorflow生成多个tfrecord文件实例
Feb 17 Python
python实现低通滤波器代码
Feb 26 Python
解决Python数据可视化中文部分显示方块问题
May 16 Python
Sentry错误日志监控使用方法解析
Nov 12 Python
python pyg2plot的原理知识点总结
Feb 28 Python
Python实现好友全头像的拼接实例(推荐)
Jun 24 #Python
Python实现的爬虫功能代码
Jun 24 #Python
python3操作mysql数据库的方法
Jun 23 #Python
Python 中pandas.read_excel详细介绍
Jun 23 #Python
python3.4用函数操作mysql5.7数据库
Jun 23 #Python
Python实现树的先序、中序、后序排序算法示例
Jun 23 #Python
详解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和区别
Jun 23 #Python
You might like
php实现在新浪云中使用imagick生成缩略图并上传的方法
2016/09/26 PHP
javascript编码的几个方法详细介绍
2013/01/06 Javascript
JavaScript模拟push
2016/03/06 Javascript
js+html5实现canvas绘制椭圆形图案的方法
2016/05/21 Javascript
第六篇Bootstrap表格样式介绍
2016/06/21 Javascript
js控住DOM实现发布微博效果
2016/08/30 Javascript
使用jquery.qrcode.js生成二维码插件
2016/10/17 Javascript
loading动画特效小结
2017/01/22 Javascript
使用原生的javascript来实现轮播图
2017/02/24 Javascript
jQuery插件zTree实现删除树节点的方法示例
2017/03/08 Javascript
ajax实现加载页面、删除、查看详细信息 bootstrap美化页面!
2017/03/14 Javascript
jQuery EasyUI 组件加上“清除”功能实例详解
2017/04/11 jQuery
vue2.0父子组件间通信的实现方法
2017/04/19 Javascript
微信小程序页面滚动到指定位置代码实例
2019/09/07 Javascript
详解node和ES6的模块导出与导入
2020/02/19 Javascript
Vue路由守卫及页面登录权限控制的设置方法(两种)
2020/03/31 Javascript
[57:16]2014 DOTA2华西杯精英邀请赛 5 25 LGD VS VG第二场
2014/05/26 DOTA
[37:22]DOTA2上海特级锦标赛D组资格赛#2 Liquid VS VP第一局
2016/02/28 DOTA
python正则表达式抓取成语网站
2013/11/20 Python
python实现斐波那契递归函数的方法
2014/09/08 Python
python多线程threading.Lock锁用法实例
2014/11/01 Python
Python中matplotlib中文乱码解决办法
2017/05/12 Python
微信跳一跳辅助python代码实现
2018/01/05 Python
python中set()函数简介及实例解析
2018/01/09 Python
python的socket编程入门
2018/01/29 Python
python中实现将多个print输出合成一个数组
2018/04/19 Python
点球小游戏python脚本
2018/05/22 Python
python使用Turtle库绘制动态钟表
2018/11/19 Python
python3实现在二叉树中找出和为某一值的所有路径(推荐)
2019/12/26 Python
windows系统Tensorflow2.x简单安装记录(图文)
2021/01/18 Python
浅谈css3中的渐进增强和优雅降级
2017/12/01 HTML / CSS
美国最大的家庭鞋类零售商之一:Shoe Carnival
2017/10/06 全球购物
英语专业学子个人的自我评价
2013/10/02 职场文书
4S店售后客服自我评价
2014/04/09 职场文书
社区优秀志愿者先进事迹
2014/05/09 职场文书
离婚协议书范文
2015/01/26 职场文书