首页 > 其他 > 详细

re模块

时间:2019-07-28 10:14:10      阅读:59      评论:0      收藏:0      [点我收藏+]

技术分享图片

技术分享图片

一:re.compile(pattern, flags=0)

   模块提供了 re.compile() 函数将一个字符串编译成 pattern object,用于匹配或搜索,re.compile() 还接受一个可选的参数 flag,用于指定正则匹配的模式

re.compile()就是要匹配的东西的对象

二:re.match(string, pos, endpos)

  匹配从 pos 到 endpos 的字符子串的开头。匹配成功返回一个 match object,不匹配返回 None。

  pos 的默认值是0,endpos 的默认值是 len(string),所以默认情况下是匹配整个字符串的开头。

二:match的更多用法

match.group([group1, ...])

  返回 match object 中的字符串。
  每一个 ( ) 都是一个分组,分组编号从1开始
  组 0 总是存在的,它就是整个表达式 。
  没有参数时,group默认为0,这时返回整个匹配到的字符串。
  指定一个参数(整数)时,返回该分组匹配到的字符串。
  指定多个参数时,返回由那几个分组匹配到的字符串组成的 tuple

技术分享图片
1 import re
2 pattern=re.compile(li)
3 print(pattern.match(lithis is flag))#只匹配开头为li的
4 print(pattern.match(thisli do not link))
5 print(pattern.match(this li my web,5))#指定开始位置匹配开头为li的
用法
技术分享图片
 1 pattern=re.compile(r(\w+) (\w+))
 2 res=pattern.match("Keven Durant,Warrior")
 3 print(res)
 4 print(res.group())#默认下返回所有匹配的数据
 5 print(res.group(1))#返回匹配到的组1的数据
 6 print(res.group(1,2))#返回匹配到的组1,2的数据
 7 ---------------------------------------------结果
 8 <re.Match object; span=(0, 12), match=Keven Durant>
 9 Keven Durant
10 Keven
11 (Keven, Durant)
用法

match.groups()

  • 返回由所有分组匹配到的字符串组成的 tuple。

match.start([group])

  • 没有参数时,返回匹配到的字符串的起始位置。
  • 指定参数(整数)时,返回该分组匹配到的字符串的起始位置。
技术分享图片
1 pattern=re.compile(r(\w+) (\w+))
2 res=pattern.match("Keven Durant,Warrior")
3 print(res.start())#返回匹配到的字符串的开始位置#0
4 print(res.start(2))#返回匹配到组的起始位置#6
用法

match.end([group])

  • 没有参数时,返回匹配到的字符串的结束位置。
  • 指定参数(整数)时,返回该分组匹配到的字符串的结束位置。
技术分享图片
1 pattern=re.compile(r(\w+) (\w+))
2 res=pattern.match("Keven Durant,Warrior")
3 print(res.end())#12    
4 print(res.end(1))#5
用法

match.span([group])

  • 返回一个二元 tuple 表示匹配到的字符串的范围,即 (start, end)。
  • 指定参数时,返回该分组匹配到的字符串的 (start, end)。
技术分享图片
1 pattern = re.compile(r"(\w+) (\w+)")
2 m = pattern.match("Kobe Bryant, Lakers")
3 print(m.span())     # (0, 11)
4 print(m.span(2))    # (5, 11)
用法

三:re.search(string, pos, endpos)

  扫描整个字符串,并返回它找到的第一个匹配(Match object)。

  和 re.match() 一样,可以通过 pos 和 endpos 指定范围

技术分享图片
1 pattern=re.compile(is)
2 print(pattern.search(this is my web,it is extemely bad))
3 
4 <re.Match object; span=(2, 4), match=is>
用法

四:re.findall(string, pos, endpos)

  找到所有匹配的子串,并返回一个 list 。

  可选参数 pos 和 endpos 同上。

技术分享图片
1 pattern=re.compile(is)
2 print(pattern.findall(this is my web,it is so cool))
用法

五:re.finditer(string, pos, endpos)

  找到所有匹配的子串,并返回由这些匹配结果(match object)组成的迭代器。

  可选参数 pos 和 endpos 和上面一样。

技术分享图片
 1 pattern=re.compile(is)
 2 res=pattern.finditer(this is my girl,she is so shy)
 3 print(res)
 4 for item in res:
 5     print(item)
 6 结果
 7 <callable_iterator object at 0x000001E5D2E78288>
 8 <re.Match object; span=(2, 4), match=is>
 9 <re.Match object; span=(5, 7), match=is>
10 <re.Match object; span=(20, 22), match=is>
用法

 

re模块

原文:https://www.cnblogs.com/Mr-l/p/11257908.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!