「Python入門」re.match()とre.search()を使うサンプル
pythonコード
#!/usr/bin/python
import re
line = “Cats are smarter than dogs";
matchObj = re.match( r’dogs’, line, re.M|re.I)
if matchObj:
print “match –> matchObj.group() : “, matchObj.group()
else:
print “No match!!"
matchObj = re.search( r’dogs’, line, re.M|re.I)
if matchObj:
print “search –> matchObj.group() : “, matchObj.group()
else:
print “No match!!"
実行結果
No match!!
search –> matchObj.group() : dogs