簡體   English   中英

re.findall和迭代式re.search之間的_qualitative_區別是什么?

[英]What is the _qualitative_ difference between re.findall and an iterative re.search?

當要查找並處理字符串中所有出現的正則表達式時, re.findall和對其返回的列表進行迭代與直接對re.search迭代之間在質上有何區別?

在代碼中, findall版本之間的質性區別是什么:

sbls = "some big ... long string"
for i in some_regex.findall( sbls )
    process_item( i )

和迭代search版本:

sbls = "some big ... long string"
m = some_regex.search( sbls )
while m:
    process_item( m.group() )
    m = some_regex.search(sbls, m.end())

在一個非常大的字符串和選擇不當/低基數的正則表達式,將在findall版本消耗更多的內存(可能通過名單)? 相反, search版本會花費更多時間嗎?

取決於我可以使用的硬件的限制,我無法辨別出不可忽略的差異,因此非常感謝其他人的見解。

就像@Jan所說的那樣,查找所有內容的速度稍快一些,但是后面的操作可以讓您更好地控制每次比賽的方式,例如,也許您盡早找到了想要的東西,就可以省去並節省尋找剩余比賽的精力。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM