If you always need to know the length, and you just need the content of the match, and not other information, you can use re.findall . Otherwise, if you only need the length, you can use, for example,
matches = re.finditer(...) ... matches = tuple(matches)
to save the iteration of matches in a reusable tuple. Then just len(matches) .
Another option, if you just need to know the total score after performing any match objects, is to use
matches = enumerate(re.finditer(...))
which will return a pair (index, match) for each of the original matches. So, you can just save the first element of each tuple in some variable.
But if you need length first and you need matching objects, not just strings, you should just do
matches = tuple(re.finditer(...))
intuited Oct 09 '10 at 4:05 2010-10-09 04:05
source share