简体   繁体   中英

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

When one wants to find and process all occurrences of a regex in a string, what is the qualitative difference between re.findall and iterating over its returned list versus iterating directly with re.search ?

In code, what is the qualitative difference between the findall version:

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

And the iterative search version:

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

In a very large string and a poorly chosen/low-cardinality regex, would the findall version consume more memory (perhaps via the list)? Conversely, would the search version take noticeably more time?

Up to the constraints of the hardware to which I have access, I have not been able to discern a non-negligible difference, so would much appreciate others' insights.

就像@Jan所说的那样,查找所有内容的速度稍快一些,但是后面的操作可以让您更好地控制每次比赛的方式,例如,也许您尽早找到了想要的东西,就可以省去并节省寻找剩余比赛的精力。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM