「python入門」re.match()の正規表現の使い方

1.pythonコード
#!/usr/bin/python3
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 (“マッチしません”)

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

Comments are closed.