python3 re.compile_Python3中正则模块re.compile、re.match及re.search函数用法详解

Python3中正则模块re.compile、re.match及re.search函数用法

re模块 re.compile、re.match、 re.search

正则匹配的时候,第一个字符是 r,表示 raw string 原生字符,意在声明字符串中间的特殊字符不用转义。

比如表示 ‘\n',可以写 r'\n',或者不适用原生字符 ‘\n'。

推荐使用 re.match

re.compile() 函数

编译正则表达式模式,返回一个对象。可以把常用的正则表达式编译成正则表达式对象,方便后续调用及提高效率。

re.compile(pattern, flags=0)

pattern 指定编译时的表达式字符串

flags 编译标志位,用来修改正则表达式的匹配方式。支持 re.L|re.M 同时匹配

flags 标志位参数

re.I(re.IGNORECASE)

使匹配对大小写不敏感

re.L(re.LOCAL)

做本地化识别(locale-aware)匹配

re.M(re.MULTILINE)

多行匹配,影响 ^ 和 $

re.S(re.DOTALL)

使 . 匹配包括换行在内的所有字符

re.U(re.UNICODE)

根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B.

re.X(re.VERBOSE)

该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。

示例:

1 importre2 content = 'Citizen wang , always fall in love with neighbour,WANG'

3 rr = re.compile(r'wan\w', re.I) #不区分大小写

4 print(type(rr))5 a =rr.findall(content)6 print(type(a))7 print(a)

findall 返回的是一个 list 对象

['wang', 'WANG']

re.match() 函数

总是从字符串‘开头曲匹配',并返回匹配的字符串的 match 对象 。

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

pattern 匹配模式,由 re.compile 获得

string 需要匹配的字符串

1 importre2 pattern = re.compile(r'hello')3 a = re.match(pattern, 'hello world')4 b = re.match(pattern, 'world hello')5 c = re.match(pattern, 'hell')6 d = re.match(pattern, 'hello')7 ifa:8 print(a.group())9 else:10 print('a 失败')11 ifb:12 print(b.group())13 else:14 print('b 失败')15 ifc:16 print(c.group())17 else:18 print('c 失败')19 ifd:20 print(d.group())21 else:22 print('d 失败')

运行结果:

hello

b 失败

c 失败

hello

match 的方法和属性

1 importre2 str = 'hello world! hello python'

3 pattern = re.compile(r'(?Phell\w)(?P\s)(?P.*ld!)') #分组,0 组是整个 hello world!, 1组 hello,2组 ld!

4 match =re.match(pattern, str)5 print('group 0:', match.group(0)) #匹配 0 组,整个字符串

6 print('group 1:', match.group(1)) #匹配第一组,hello

7 print('group 2:', match.group(2)) #匹配第二组,空格

8 print('group 3:', match.group(3)) #匹配第三组,ld!

9 print('groups:', match.groups()) #groups 方法,返回一个包含所有分组匹配的元组

10 print('start 0:', match.start(0), 'end 0:', match.end(0)) #整个匹配开始和结束的索引值

11 print('start 1:', match.start(1), 'end 1:', match.end(1)) #第一组开始和结束的索引值

12 print('start 2:', match.start(1), 'end 2:', match.end(2)) #第二组开始和结束的索引值

13 print('pos 开始于:', match.pos)14 print('endpos 结束于:', match.endpos) #string 的长度

15 print('lastgroup 最后一个被捕获的分组的名字:', match.lastgroup)16 print('lastindex 最后一个分组在文本中的索引:', match.lastindex)17 print('string 匹配时候使用的文本:', match.string)18 print('re 匹配时候使用的 Pattern 对象:', match.re)19 print('span 返回分组匹配的 index (start(group),end(group)):', match.span(2))

运行结果:

1 group 0: hello world!2 group 1: hello3 group 2:4 group 3: world!5 groups: ('hello', ' ', 'world!')6 start 0: 0 end 0: 12

7 start 1: 0 end 1: 5

8 start 2: 0 end 2: 6

9 pos 开始于: 010 endpos 结束于: 25

11 lastgroup 最后一个被捕获的分组的名字: last12 lastindex 最后一个分组在文本中的索引: 3

13 string 匹配时候使用的文本: hello world! hello python14 re 匹配时候使用的 Pattern 对象: re.compile('(?Phell\\w)(?P\\s)(?P.*ld!)')15 span 返回分组匹配的 index (start(group),end(group)): (5, 6)

re.search 函数

对整个字符串进行搜索匹配,返回第一个匹配的字符串的 match 对象。

re.search(pattern, string[, flags=0])

pattern 匹配模式,由 re.compile 获得

string 需要匹配的字符串

1 importre2 str = 'say hello world! hello python'

3 pattern = re.compile(r'(?Phell\w)(?P\s)(?P.*ld!)') #分组,0 组是整个 hello world!, 1组 hello,2组 ld!

4 search =re.search(pattern, str)5 print('group 0:', search.group(0)) #匹配 0 组,整个字符串

6 print('group 1:', search.group(1)) #匹配第一组,hello

7 print('group 2:', search.group(2)) #匹配第二组,空格

8 print('group 3:', search.group(3)) #匹配第三组,ld!

9 print('groups:', search.groups()) #groups 方法,返回一个包含所有分组匹配的元组

10 print('start 0:', search.start(0), 'end 0:', search.end(0)) #整个匹配开始和结束的索引值

11 print('start 1:', search.start(1), 'end 1:', search.end(1)) #第一组开始和结束的索引值

12 print('start 2:', search.start(1), 'end 2:', search.end(2)) #第二组开始和结束的索引值

13 print('pos 开始于:', search.pos)14 print('endpos 结束于:', search.endpos) #string 的长度

15 print('lastgroup 最后一个被捕获的分组的名字:', search.lastgroup)16 print('lastindex 最后一个分组在文本中的索引:', search.lastindex)17 print('string 匹配时候使用的文本:', search.string)18 print('re 匹配时候使用的 Pattern 对象:', search.re)19 print('span 返回分组匹配的 index (start(group),end(group)):', search.span(2))

注意 re.search 和 re.match 匹配的 str 的区别

运行结果:

1 group 0: hello world!2 group 1: hello3 group 2:4 group 3: world!5 groups: ('hello', ' ', 'world!')6 start 0: 4 end 0: 16

7 start 1: 4 end 1: 9

8 start 2: 4 end 2: 10

9 pos 开始于: 010 endpos 结束于: 29

11 lastgroup 最后一个被捕获的分组的名字: last12 lastindex 最后一个分组在文本中的索引: 3

13 string 匹配时候使用的文本: say hello world! hello python14 re 匹配时候使用的 Pattern 对象: re.compile('(?Phell\\w)(?P\\s)(?P.*ld!)')15 span 返回分组匹配的 index (start(group),end(group)): (9, 10)

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值