python正则group用法演示
下面的python代码分析一个句子取出指定位置的单词,详细演示了group的用法
""" 皮蛋编程(https://www.pidancode.com) 创建日期:2022/4/2 功能描述:python正则group用法演示 """ import re line = "Cats are smarter than dogs" 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
相关文章