若是你們在網上搜索Python 正則表達式
,你將會看到大量的垃圾文章會這樣寫代碼:python
import re
pattern = re.compile('正則表達式')
text = '一段字符串'
result = pattern.findall(text)
複製代碼
這些文章的做者,多是被其餘語言的壞習慣影響了,也多是被其餘垃圾文章誤導了,不假思索拿來就用。正則表達式
在Python裏面,真的不須要使用re.compile!express
爲了證實這一點,咱們來看Python的源代碼。緩存
在PyCharm裏面輸入:微信
import re
re.search
複製代碼
而後Windows用戶按住鍵盤上的Ctrl鍵,鼠標左鍵點擊search
,Mac用戶按住鍵盤上的Command鍵,鼠標左鍵點擊search
,PyCharm會自動跳轉到Python的re模塊。在這裏,你會看到咱們經常使用的正則表達式方法,不管是findall
仍是search
仍是sub
仍是match
,所有都是這樣寫的:app
_compile(pattern, flag).對應的方法(string)
複製代碼
例如:this
def findall(pattern, string, flags=0):
"""Return a list of all non-overlapping matches in the string. If one or more capturing groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result."""
return _compile(pattern, flags).findall(string)
複製代碼
以下圖所示:spa
而後咱們再來看compile
:3d
def compile(pattern, flags=0):
"Compile a regular expression pattern, returning a Pattern object."
return _compile(pattern, flags)
複製代碼
以下圖所示:code
看出問題來了嗎?
咱們經常使用的正則表達式方法,都已經自帶了compile
了!
根本沒有必要畫蛇添足先re.compile
再調用正則表達式方法。
此時,可能會有人反駁:
若是我有一百萬條字符串,使用使用某一個正則表達式去匹配,那麼我能夠這樣寫代碼:
texts = [包含一百萬個字符串的列表]
pattern = re.compile('正則表達式')
for text in texts:
pattern.search(text)
複製代碼
這個時候,re.compile
只執行了1次,而若是你像下面這樣寫代碼:
texts = [包含一百萬個字符串的列表]
for text in texts:
re.search('正則表達式', text)
複製代碼
至關於你在底層對同一個正則表達式執行了100萬次re.compile
。
Talk is cheap, show me the code.
咱們來看源代碼,正則表達式re.compile
調用的是_compile
,咱們就去看_compile
的源代碼,以下圖所示:
紅框中的代碼,說明了_compile
自帶緩存。它會自動儲存最多512條由type(pattern), pattern, flags)組成的Key,只要是同一個正則表達式,同一個flag,那麼調用兩次_compile時,第二次會直接讀取緩存。
綜上所述,請你不要再手動調用re.compile
了,這是從其餘語言(對的,我說的就是Java)帶過來的陋習。
若是這篇文章對你有幫助,請關注個人微信公衆號: 未聞Code(ID: itskingname),第一時間獲的最新更新: