python 3.x 匹配match和search的區別

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'

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章