match是從標的的開頭部分進行匹配,如存在,則返回第一次符合對象。
search是從標的的任意部分匹配,如存在,則返回第一次符合對象。
match例子1:
bt = 'bat|bet|bit'
m = re.match(bt,'bat bet blt')
if m is not None:
m.group()
運行結果:
'bat'
match例子2:
bt = 'bat|bet|bit'
m = re.match(bt,'blt bat bet blt')
if m is not None:
m.group()
運行結果爲空。
search例子1:
bt = 'bat|bet|bit'
m = re.search(bt,'bat bet blt')
if m is not None:
m.group()
運行結果:
'bat'
search例子2:
bt = 'bat|bet|bit'
m = re.search(bt,'blt bat bet blt')
if m is not None:
m.group()
運行結果:
'bat'