re模块(* * * * *)

就其本质而言,正则表达式(或 RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现。正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹配引擎执行。

字符匹配(普通字符,元字符):

1 普通字符:大多数字符和字母都会和自身匹配
              >>> re.findall('alvin','yuanaleSxalexwupeiqi')
                      ['alvin']

2 元字符:.(匹配所有)

^只(从头开始匹配)

$(必须在最后匹配)

*(重复匹配)(0到无穷)

n2="kuisddddss"

>>> re.findall('d*',n2)
['', '', '', '', 'dddd', '', '', '']

>>> n3="asdhfale"
>>> re.findall("alex*",n3)
['ale']

+(重复匹配)(1到无穷)

>>> n3="asdhfale"

>>> re.findall("alex+",n3)
[]

?(重复匹配)(0或1次)

n4 =  "asdhfale"

>>> re.findall("alex?",n4)
['ale']

n5=  "asdhfalexxxxx"

>>> re.findall("alex?",n5)
['alex']

{ }自定义次数,如{0,}=*,{6}固定6次,{0,6}0到6次

[ ]或 比如x[yz] 匹配xy或xz  其它符号在[ ] 中皆为普通字符。除了-,比如[a-z]匹配a到z,和^比如[^a-z]匹配非a到z,和\转义符,比如\(\)

|或,比如ka|b,匹配ka或b

re.match类似serach加一个^

sub:替换,subn,结果显示替换次数

>>> re.sub(r"\d","a"," ki88s1i")
' kiaasai'

>>> re.subn(r"\d","a"," ki88s1i")
(' kiaasai', 3)

compile编辑规则

ssa=re.compile("\d+")

>>> ssa.findall("sdii888")
['888']

去优先级:

比如:(findall优先匹配组里的信息)

re.findall(r'www\.(baidu|3dm)\.com',"www.3dm.com")
['3dm']

去优先级:?:

>>> re.findall(r'www\.(?: baidu|3dm)\.com',"www.3dm.com")
['www.3dm.com']

再如

>>> re.findall(r'(abc)+',"abcabcabc")

['abc']

如何取出全部呢?依然是去优先级

>>> re.findall(r'(abc)+',"abcabcabc")
['abc']

 

元字符之. ^ $ * + ? { }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import re
 
ret=re.findall('a..in','helloalvin')
print(ret)#['alvin']
 
 
ret=re.findall('^a...n','alvinhelloawwwn')
print(ret)#['alvin']
 
 
ret=re.findall('a...n$','alvinhelloawwwn')
print(ret)#['awwwn']
 
 
ret=re.findall('a...n$','alvinhelloawwwn')
print(ret)#['awwwn']
 
 
ret=re.findall('abc*','abcccc')#贪婪匹配[0,+oo]  
print(ret)#['abcccc']
 
ret=re.findall('abc+','abccc')#[1,+oo]
print(ret)#['abccc']
 
ret=re.findall('abc?','abccc')#[0,1]
print(ret)#['abc']
 
 
ret=re.findall('abc{1,4}','abccc')
print(ret)#['abccc'] 贪婪匹配

注意:前面的*,+,?等都是贪婪匹配,也就是尽可能匹配,后面加?号使其变成惰性匹配

1
2
ret=re.findall('abc*?','abcccccc')
print(ret)#['ab']

元字符之字符集[]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#--------------------------------------------字符集[]
ret=re.findall('a[bc]d','acd')
print(ret)#['acd']
 
ret=re.findall('[a-z]','acd')
print(ret)#['a', 'c', 'd']
 
ret=re.findall('[.*+]','a.cd+')
print(ret)#['.', '+']
 
#在字符集里有功能的符号: - ^ \
 
ret=re.findall('[1-9]','45dha3')
print(ret)#['4', '5', '3']
 
ret=re.findall('[^ab]','45bdha3')
print(ret)#['4', '5', 'd', 'h', '3']
 
ret=re.findall('[\d]','45bdha3')
print(ret)#['4', '5', '3']

元字符之转义符\

反斜杠后边跟元字符去除特殊功能,比如\.
反斜杠后边跟普通字符实现特殊功能,比如\d

\d  匹配任何十进制数;它相当于类 [0-9]。
\D 匹配任何非数字字符;它相当于类 [^0-9]。
\s  匹配任何空白字符;它相当于类 [ \t\n\r\f\v]。
\S 匹配任何非空白字符;它相当于类 [^ \t\n\r\f\v]。
\w 匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]。
\W 匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_]
\b  匹配一个特殊字符边界,比如空格 ,&,#等

1
2
3
4
ret=re.findall('I\b','I am LIST')
print(ret)#[]
ret=re.findall(r'I\b','I am LIST')
print(ret)#['I']

现在我们聊一聊\,先看下面两个匹配:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#-----------------------------eg1:
import re
ret=re.findall('c\l','abc\le')
print(ret)#[]
ret=re.findall('c\\l','abc\le')
print(ret)#[]
ret=re.findall('c\\\\l','abc\le')
print(ret)#['c\\l']
ret=re.findall(r'c\\l','abc\le')
print(ret)#['c\\l']
 
#-----------------------------eg2:
#之所以选择\b是因为\b在ASCII表中是有意义的
= re.findall('\bblow''blow')
print(m)
= re.findall(r'\bblow''blow')
print(m)

  

元字符之分组()

1
2
3
4
5
6
= re.findall(r'(ad)+''add')
print(m)
 
ret=re.search('(?P<id>\d{2})/(?P<name>\w{3})','23/com')
print(ret.group())#23/com
print(ret.group('id'))#23

元字符之|

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import re
 
ret=re.findall('a..in','helloalvin')
print(ret)#['alvin']
 
 
ret=re.findall('^a...n','alvinhelloawwwn')
print(ret)#['alvin']
 
 
ret=re.findall('a...n$','alvinhelloawwwn')
print(ret)#['awwwn']
 
 
ret=re.findall('a...n$','alvinhelloawwwn')
print(ret)#['awwwn']
 
 
ret=re.findall('abc*','abcccc')#贪婪匹配[0,+oo]  
print(ret)#['abcccc']
 
ret=re.findall('abc+','abccc')#[1,+oo]
print(ret)#['abccc']
 
ret=re.findall('abc?','abccc')#[0,1]
print(ret)#['abc']
 
 
ret=re.findall('abc{1,4}','abccc')
print(ret)#['abccc'] 贪婪匹配

注意:前面的*,+,?等都是贪婪匹配,也就是尽可能匹配,后面加?号使其变成惰性匹配

1
2
ret=re.findall('abc*?','abcccccc')
print(ret)#['ab']

元字符之字符集[]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#--------------------------------------------字符集[]
ret=re.findall('a[bc]d','acd')
print(ret)#['acd']
 
ret=re.findall('[a-z]','acd')
print(ret)#['a', 'c', 'd']
 
ret=re.findall('[.*+]','a.cd+')
print(ret)#['.', '+']
 
#在字符集里有功能的符号: - ^ \
 
ret=re.findall('[1-9]','45dha3')
print(ret)#['4', '5', '3']
 
ret=re.findall('[^ab]','45bdha3')
print(ret)#['4', '5', 'd', 'h', '3']
 
ret=re.findall('[\d]','45bdha3')
print(ret)#['4', '5', '3']

元字符之转义符\

反斜杠后边跟元字符去除特殊功能,比如\.
反斜杠后边跟普通字符实现特殊功能,比如\d

\d  匹配任何十进制数;它相当于类 [0-9]。
\D 匹配任何非数字字符;它相当于类 [^0-9]。
\s  匹配任何空白字符;它相当于类 [ \t\n\r\f\v]。
\S 匹配任何非空白字符;它相当于类 [^ \t\n\r\f\v]。
\w 匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]。
\W 匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_]
\b  匹配一个特殊字符边界,比如空格 ,&,#等

1
2
3
4
ret=re.findall('I\b','I am LIST')
print(ret)#[]
ret=re.findall(r'I\b','I am LIST')
print(ret)#['I']

现在我们聊一聊\,先看下面两个匹配:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#-----------------------------eg1:
import re
ret=re.findall('c\l','abc\le')
print(ret)#[]
ret=re.findall('c\\l','abc\le')
print(ret)#[]
ret=re.findall('c\\\\l','abc\le')
print(ret)#['c\\l']
ret=re.findall(r'c\\l','abc\le')
print(ret)#['c\\l']
 
#-----------------------------eg2:
#之所以选择\b是因为\b在ASCII表中是有意义的
= re.findall('\bblow''blow')
print(m)
= re.findall(r'\bblow''blow')
print(m)

  

元字符之分组()

1
2
3
4
5
6
= re.findall(r'(ad)+''add')
print(m)
 
ret=re.search('(?P<id>\d{2})/(?P<name>\w{3})','23/com')
print(ret.group())#23/com
print(ret.group('id'))#23

python中的RE模块的更多相关文章

  1. Python中的random模块,来自于Capricorn的实验室

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  2. Python中的logging模块

    http://python.jobbole.com/86887/ 最近修改了项目里的logging相关功能,用到了python标准库里的logging模块,在此做一些记录.主要是从官方文档和stack ...

  3. Python中的random模块

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  4. 浅析Python中的struct模块

    最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概了解了,在这里做一下简单的总结. 了解c语言 ...

  5. python中的StringIO模块

    python中的StringIO模块 标签:python StringIO 此模块主要用于在内存缓冲区中读写数据.模块是用类编写的,只有一个StringIO类,所以它的可用方法都在类中.此类中的大部分 ...

  6. python中的select模块

    介绍: Python中的select模块专注于I/O多路复用,提供了select  poll  epoll三个方法(其中后两个在Linux中可用,windows仅支持select),另外也提供了kqu ...

  7. Python中的re模块--正则表达式

    Python中的re模块--正则表达式 使用match从字符串开头匹配 以匹配国内手机号为例,通常手机号为11位,以1开头.大概是这样13509094747,(这个号码是我随便写的,请不要拨打),我们 ...

  8. python中的shutil模块

    目录 python中的shutil模块 目录和文件操作 归档操作 python中的shutil模块 shutil模块对文件和文件集合提供了许多高级操作,特别是提供了支持文件复制和删除的函数. 目录和文 ...

  9. Python中使用operator模块实现对象的多级排序

    Python中使用operator模块实现对象的多级排序 今天碰到一个小的排序问题,需要按嵌套对象的多个属性来排序,于是发现了Python里的operator模块和sorted函数组合可以实现这个功能 ...

  10. 【转】浅析Python中的struct模块

    [转]浅析Python中的struct模块 最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概 ...

随机推荐

  1. CSS规范—分类方法(NEC规范学习笔记)

    一.CSS文件的分类和引用顺序 Css按照性质和用途,将Css文件分成“公共型样式”.“特殊型样式”.“皮肤型样式”,并以此顺序引用,有需要可以添加版本号 1.公共型样式:包含以下几个部分 标签的重置 ...

  2. js的观察者模式

    观察者模式(发布-订阅模式):它定义对象间一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知.由主体和观察者组成,主体负责发布事件,同时观察者通过订阅这些事件来观察该主体 ...

  3. PHP下载微信头像

    public function downloadPic($openid='',$headimgurl='') { $header = array( 'User-Agent: Mozilla/5.0 ( ...

  4. Qt坑点汇总

    1.场景:假如我们想在layout中的qlabel中设置一个图片 1.1 如果简单地使用border-image,我们可以做到,并且拖动界面时,label可以随布局正常变化,这里需要注意的是,修改ui ...

  5. ejabberd之开题篇

    ejabberd是基于Jabber/XMPP协议的即时通讯服务器.采用Erlang/OTP开发.它的特点是,跨平台,容错,集群和模块化.     因为自己本身是基于erlang工作,而且听到有不少的公 ...

  6. mysql数据库的增量备份和全备

    还有一种简单的方法 参考 https://blog.csdn.net/u010098331/article/details/50932064 (注意:5.6版本以上新加了gtid 功能,gtid开启之 ...

  7. vim 加密(crypt)文本文档

    算法 vim7.3版本支持两种加密方式——PKzip算法(已知有缺陷的).Blowfish算法(从7.3版本开始支持).Blowfish2算法(从7.4.399版本开始支持)而vim -x 默认采用P ...

  8. videojs播放直播源rtmp时画面在左上角解决方案

    问题描述:https://stackoverflow.com/questions/30383135/videojs-live-rtmp-stream-player-and-video-size-iss ...

  9. PKU《程序设计》专项课程_递归汉诺塔问题

    取自coursera.org上公开课北京大学<C程序设计进阶> 递归调用注意的点 1.关注点放在求解的目标上,递推是,目标放在开头 2.找到第N次和第(N-1)次之间的关系,通项公式 3. ...

  10. 渗透测试学习 三、Linux基础

    Linux发行版本  内核+应用程序  打包在一起 一.优点: 完全免费 完全兼容POSIX 1.0标准 多用户,多任务 良好的界面 可靠安全稳定的性能 支持多种平台 丰富的网络功能 安全性更好(针对 ...