「python入門」re.search()の正規表現の使い方
1.pythonコード
#!/usr/bin/python3
import re
line = “Cats are smarter than dogs";
searchObj = re.search( r'(.*) are (.*?) .*’, line, re.M|re.I)
if searchObj:
print (“searchObj.group() : “, searchObj.group())
print (“searchObj.group(1) : “, searchObj.group(1))
print (“searchObj.group(2) : “, searchObj.group(2))
else:
print (“Nothing found!!")
結果:
searchObj.group() : Cats are smarter than dogs
searchObj.group(1) : Cats
searchObj.group(2) : smarter