python表达式3 5_(五)5-3Python正则表达式

本文介绍了Python中正则表达式的match, search, split, findall和finditer方法的使用,包括实例演示和关键概念解析。重点讲解了如何通过这些方法匹配、搜索和操作字符串,并展示了group, groupdict等高级特性。
摘要由CSDN通过智能技术生成

正则对象的方法

1、match 方法

importre

reg= re.compile(r'(hello w.*)(hello cn.*)')

a= 'hello world hello cnblogs'result=reg.match(a)print(result)

b= "aa" +aprint(b)

result2=reg.match(b)print(result2)

运行结果:

<_sre.SRE_Match object at 0x00000000025E21C8>hello world hello cnblogs

aahello world hello cnblogs

None

2、search方法

importre

reg= re.compile(r'(hello w.*)(hello cn.*)')

a= 'hello world hello cnblogs'b= "aa" +aprint(b)

result3=reg.search(b)print(result3)print(result3.group())

运行结果:

aahello world hello cnblogs<_sre.SRE_Match object at 0x0000000002592140>hello world hello cnblogs

3、正则对象的split方法

importre

p= re.compile(r'\d+')print(p.split('one1two2three3four4'))

运行结果:

['one', 'two', 'three', 'four', '']

注:直接把p的正则当成分隔符,然后把最后的字符串用p进行分割,最终返回结果

4、正则对象的findall方法

findall(string[,pos[,endpos]])

搜索string,以列表形式返回全部能匹配的字符串

importre

p= re.compile(r'\d+')print(p.findall('one1two2three3four4'))

运行结果

['1', '2', '3', '4']

注:findall把匹配的字符串最后一列表的形式返回

5、正则对象的finditer方法

finditer(string[,pos[,endpos]])

搜索string,返回一个顺序访问每一个结果(match对象)的迭代器

importre

p= re.compile(r'\d+')

a_string= 'one1two2three3four4'

for i inp.finditer(a_string):#print(i)

print(type(i))print(i.group())

运行结果:

1

2

3

4

注:p.finditer(a_string)是一个迭代器,返回的每个m都是match对象

6、正则对象的sub方法

sub(pattern, repl, string, count=0)

pattern : 正则中的模式字符串。

repl : 替换的字符串,也可为一个函数。

string : 要被查找替换的原始字符串。

count : 模式匹配后替换的最大次数,默认 0 表示替换所有的匹配。

match匹配对象

importre

reg= re.compile(r'(?Pabc)(.*)(?P=tagname)')

result= reg.match('abclfjasdasda234hhkhabc')print(dir(result))print(result)print(result.groups())print(result.group(2))print(result.group('tagname'))print('*'*10)print(result.groupdict())

运行结果:

['__class__', '__copy__', '__deepcopy__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'end', 'endpos', 'expand', 'group', 'groupdict', 'groups', 'lastgroup', 'lastindex', 'pos', 're', 'regs', 'span', 'start', 'string']<_sre.SRE_Match object at 0x0000000002662140>('abc', 'lfjasdasda234hhkh')

lfjasdasda234hhkh

abc**********{'tagname': 'abc'}

注:

1、 result 由字符串转换成了正则对象

2、 result.groups()是所有的匹配到的数据,每个()是一个元素,最终返回一个tuple

3、 group()可以通过下标(从1开始)的方式访问,也可以通过分组名进行访问

4、 groupdict只能显示有分组名的数据

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值