Python3的re.match函数怎么用
更新:HHH   时间:2023-1-7


本篇内容主要讲解“Python3的re.match函数怎么用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python3的re.match函数怎么用”吧!

实例

#!/usr/bin/python3

import re line = "Cats are smarter than dogs"

# .* 表示任意匹配除换行符(\n、\r)之外的任何单个或多个字符

# (.*?) 表示"非贪婪"模式,只保存第一个匹配到的子串

matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I) 

if matchObj:   

print ("matchObj.group() : ", matchObj.group())   

print ("matchObj.group(1) : ", matchObj.group(1))   

print ("matchObj.group(2) : ", matchObj.group(2))

else:   

print ("No match!!")

以上实例执行结果如下:

matchObj.group() :  Cats are smarter than dogs
matchObj.group(1) :  Cats
matchObj.group(2) :  smarter

到此,相信大家对“Python3的re.match函数怎么用”有了更深的了解,不妨来实际操作一番吧!这里是天达云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

返回移动开发教程...