1. 程式人生 > >Python面試題----Python 的re模組中match、search、findall、finditer的區別

Python面試題----Python 的re模組中match、search、findall、finditer的區別

請簡要說明Python 的re模組中match、search、findall、finditer的區別


re是Python中用於正則表示式相關處理的類,這四個方法都是用於匹配字串的,具體區別如下:

match

匹配string 開頭,成功返回Match object, 失敗返回None,只匹配一個。

search

在string中進行搜尋,成功返回Match object, 失敗返回None, 只匹配一個。

findall

在string中查詢所有 匹配成功的組, 即用括號括起來的部分。返回list物件,每個list item是由每個匹配的所有組組成的list。

finditer

在string中查詢所有 匹配成功的字串, 返回iterator,每個item是一個Match object。

借鑑—亦遊的原文