re模块

作者: 低吟浅唱1990 | 来源:发表于2017-06-22 00:22 被阅读57次

re模块实现了丰富的正则表达式句法。

re.match(pattern, string, flags=0)

从源字符串的开始位置匹配一个模式

>>>text1 = 'Hello spam...World'
>>>text2 = 'Hello spam...other'
>>>matchobj = re.match('Hello(.\*)World',text1)
>>>print(matchobj)
>>><_sre.SRE_Match object; span=(0, 18), match='Hello spam...World'>

>>>matchobj = re.match('Hello(.\*)World',text2)
>>>print(matchobj)
>>>None   #无匹配返回None
Hello(.*)World--->Hello和World匹配其自身,(.*)匹配任何字符(.)重复0次到多次(*)

re.compile(pattern, flags=0)

编译一个模式对象,能够调用regex.match(string[, pos[, endpos]])和regex.match(string[, pos[, endpos]])

pattobj = re.compile('Hello(.\*)World')
matchobj = pattobj.match(text1)
print(matchobj.group(1))
>>> spam...   #匹配到的子字符串
>>>print(matchobj.group(0))   #匹配到的原始字符串
>>>Hello spam...World

字符串操作与模式

result = 'abc--bbb--ccc'
print(result.split('--'))
print(result.replace('--','..'))
>>>['abc', 'bbb', 'ccc']
>>>abc..bbb..ccc
字符串操作不能完成更加复杂的操作
>>>result = 'abc--bbb==ccc'
>>>print(result.split(['--','=='])) #出错

re.split(pattern, string, maxsplit=0, flags=0) 用pattern来分割string

result = 'abc--bbb--ccc'
print(re.split('--',result))
>>>['abc', 'bbb', 'ccc']
如果是result = 'abc--bbb==ccc'情况下要分割出字母
>>>print(re.split('--|==',result)) #在--或==的两侧分割
>>>['abc', 'bbb', 'ccc']
>>>print(re.split('(?:--)|(?:==)',result))   (?:R)表示分割开R而不代表保留下的组合
>>>['abc', 'bbb', 'ccc']

re.sub(pattern, repl, string, count=0, flags=0) 用repl替换string中被模式匹配的部分

>>>result = 'abc--bbb--ccc'
>>>print(re.sub('--','...',result))
>>>abc...bbb...ccc
>>>result = 'abc--bbb==ccc'
>>>print(re.sub('--|==','...',result))
>>>abc...bbb...ccc

re.findall(pattern, string, flags=0)

>>>result = re.findall('<(.*?)>','<spam>/<ham>/<eggs>') #找到所有匹配的组合
>>>print(result)
>>>['spam', 'ham', 'eggs']

>>>result = re.findall('<(.*?)>.*<(.*?)>','<spam> \n <ham>\n<eggs>') #在\n处停止
>>>print(result)
>>>[]
(?s)能够改变"."的匹配规则 如果开头有(?s)强制将“.”匹配到多行文本的结尾符
>>>result = re.findall('(?s)<(.\*?)>.\*<(.\*?)>','<spam> \n <ham>\n<eggs>')
>>>print(result)
>>>[('spam', 'eggs')]

re.search(pattern, string, flags=0) 扫描整个string找到第一个匹配的地址,并且返回一个match对象

>>>result = re.search('(?P<part1>\w*)/(?P<part2>\w*)','...aaa/bbb/ccc')  #(?P<part1>\w*)匹配()里面的任何正则表达式,并将一个命名过的组合分隔开
>>>print(result.groups())
>>>('aaa', 'bbb')
>>>print(result.groupdict())
>>>{'part1': 'aaa', 'part2': 'bbb'}
>>>print(result.group('part1'))
>>>aaa

R代表正则表达式,C代表字符 N表示数字

操作符 意义
. 匹配任何字符串(包括换行符,如果模式开头包含(?s))
^ 匹配字符串的开头
$ 匹配字符串的结尾
C 任何非特殊字符与本身相匹配
R* 有0到多个正则表达式
R? 有0或1个正则表达式
R{m} 匹配m个R:例如a{5} 匹配'aaaaa'
R{m,n} 匹配m到n个R
R*?,R+? 和*、+和?一样不过匹配尽可能少的字符/重复次数
R??,R{m,n}? 作符 匹配和消耗尽可能少的字符
[...] 定义字符集:[a-zA-Z]匹配所有字母
[ ^...] 定义互补字符集:匹配集合之外的字符
| 转义字符
\ 匹配\本身
RR 连接:匹配两个R
(R) 匹配()里的任何正则表达式R,并且将组合分隔开 保留匹配字符串
(?:R) 和(R)一样 但不保留组合
(?=R) 前向断言:如果R匹配下一部分则成功匹配,但不消耗字符串任何部分
(?!R) 与上相反
(?P<name>R) 匹配()里的任何表达式,并将一个命名过的组合分割开
(?P=name) 匹配与前配命名为name的组相匹配的文本
(?letter) 设置工作模式标识符
(?<=R) 后向断言:如果R匹配紧邻着的字符串当前的位置的上一部分则成功匹配
(?<!R) 与上相反

相关文章

  • 21.Python之re模块

    Python之re模块 re模块介绍re 模块使 Python 语言拥有全部的正则表达式功能。 re模块的内置方法...

  • python(学会正则走天下)

    python通过re模块来实现。本篇文章着重对Python的RE进行介绍re 模块首先通过 re.compiler...

  • Python 脚本之统计基因组文件中染色体长度及N碱基数目

    模块介绍 re模块 re模块是Python中的正则表达式调用模块,在python中,通过将正则表达式内嵌集成re模...

  • 遇见正则表达式(2)

    昨天我已经埋好了伏笔,今天来重点学习re模块。 学习re模块,主要是学习该模块的几个重要的方法。 re.finda...

  • 小猪的Python学习之旅 —— 3.正则表达式

    re模块 Python中通过re模块使用正则表达式,该模块提供的几个常用方法: 1.匹配 re.match(pat...

  • python05-正则表达式(二)

    正则表达式(二) re模块(regex) python中没有正则表达式的函数,需要引入内置的re模块 re模块方法...

  • re模块

    匹配标签 匹配整数 数字匹配 爬虫练习

  • re模块

    参考资料https://www.ibm.com/developerworks/cn/opensource/os-c...

  • re 模块

    1、Python中的模块有过C语言编程经验的朋友都知道在C语言中如果要引用sqrt函数,必须用语句#include...

  • re模块

    1、re.match函数 原型:match(pattern, string, flags=0) 参数:patter...

网友评论

      本文标题:re模块

      本文链接:https://www.haomeiwen.com/subject/vuatcxtx.html