python match对象_Python程序设计之子模式与match对象

1.match函数详解

(matchobject/None) match(pattern,string[,flags])

函数参数由模式串patter,原字符串,以及可选参数组成,patter的值有以下几种

'\\d+'#使用"\"开头的元字符实现字符串特定搜索

r'\d+'#使用r''原始字符串实现字符串特定搜索

r"(\w+) (\w+)"

"(\\d+) (\\w+)" #用子模式做匹配字符串

#子模式是指以"(正则表达式)"这种形式存在的字符串1

2

3

4

第三个可选参数有以下几种选择,可以使用"|"符号自由组合

import re

re.I#表示忽略大小写

re.L#做本地化识别(locale-aware)匹配

re.M#表示多行匹配模式

re.S#表示元字符可以匹配任何字符,包括换行符

re.U#配Unicode字符

re.X#忽略模式中空格,并可以使用#注释1

2

3

4

5

6

当match()函数返回match对象时,有以下几种方法

import re

tr='hello 20 hello 35 nihao 40 hello'

part1=re.compile(r'\d+') #使用"\"开头的元字符实现字符串特定搜索

part4=re.compile('\\s+')

resulit1=part1.search(sss,index1) #按照索引搜索

resulit2=part4.search(sss)

res=re.match("(\\w+) (\\d+)","heelo 12,hello 14")

print(res.groups(),end='\n')

res1=re.match(r"(\w+) (\w+)","heelo my hello you").groups()

print(res1,end='\n')

#print(res.groups(),end='\n') #s输出第一个元组信息

#print(resulit1.group(1),end='\n')

print(resulit1.groups(),end='\n')

print(resulit1.start(),end='\n') #返回第一次出现的位置

print(resulit2.end(),end='\n') #返回最后一次出现的位置

print(resulit1.groupdict(),end='\n') #输出字典

print(resulit2.groupdict(),end='\n')

print(resulit1.span(),end='\n') #返回字符出现开始和结束位置

print(resulit2.span(),end='\n')

print(re.match(r"(\w+) (\w+)","heelo my hello you").group(1,2))

print(re.match(r"(?P\w+) (?P\w+)","heelo my hello you").group('f','s'))1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

下面是输出结果:

('heelo', '12')

('heelo', 'my')

()

21

6

{}

{}

(21, 23)

(5, 6)

('heelo', 'my')

('heelo', 'my')1

2

3

4

5

6

7

8

9

10

11

代码:

import re

ss='hello 23 world 56 my number is 1234-5678'

print(re.match('(\\w+) (\\d+)',ss))1

2

输出

1

2.search()函数

matchobject/None search(pattern,string[,flags])

search()函数返回的是模式串的详细信息

import re

s0='135-94359171 155-12345678 hello 2122-505678'

print(re.search(r'(\d{3,4})-(\d{6,8})', s0),end='\n')1

2

结果如下:

123-1231441

3.split()函数

list split(pattern,string[,maxsplit=0])

import re

sss='first match object h 20 552'

part4=re.compile('\\s+')

print(part4.split(sss)) #按照匹配模式进行划分1

2

3

输出结果:

['first', 'match', 'object', 'h', '20', '552']

4.sub()函数

string sub(pattern,string[,count=0])

import re

sss='first match object h 20 552'

part2=re.compile('\\d+') #使用r''原始字符串实现字符串特定搜索

print(part2.sub('h',sss))

print(re.sub(part2,'h',sss)) #替换模式匹配的字符串1

2

3

4

结果

first match object h h h

first match object h h h1

5.escape()函数

string escape(string)

import re

sss='first match object h 20 552'

print(re.escape(ssss)) #将特殊字符进行转义1

2

结果:

first\ match\ object\ \\\ \\\+

6.findall()函数

list findall(pattern,string[,flags])

import re

sss='first match object h 20 552'

part1=re.compile(r'\d+') #使用"\"开头的元字符实现字符串特定搜索

part3=re.compile('\\w+')

#求所有符合条件的字符

print(part1.findall(sss)) #findall()函数是输出所符合条件的模式匹配项

print(part3.findall(sss))1

2

3

4

5

6

结果:

['20', '552']

['first', 'match', 'object', 'h', '20', '552']1

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值