Number of regular expression matches

I use the finditer function in the re module to match some things, and everything works.

Now I need to find out how many matches I have. Is this possible without repeating the iterator twice? (one to find out the quantity and then the real iteration)

Some code:

 imageMatches = re.finditer("<img src\=\"(?P<path>[-/\w\.]+)\"", response[2]) # <Here I need to get the number of matches> for imageMatch in imageMatches: doStuff 

Everything works, I just need to get the number of matches before the loop.

+58
python regex
09 Oct 2018-10-10 at
source share
6 answers

If you know that you need all matches, you can use the re.findall function. It will return a list of all matches. Then you can just do len(result) for the number of matches.

+83
Oct 09 '10 at 4:02
source share

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(...)) 
+9
Oct 09 '10 at 4:05
source share

If you find that you need to stick with finditer() , you can just use a counter when iterating through an iterator.

Example:

 >>> from re import * >>> pattern = compile(r'.ython') >>> string = 'i like python jython and dython (whatever that is)' >>> iterator = finditer(pattern, string) >>> count = 0 >>> for match in iterator: count +=1 >>> count 3 

If you need finditer() functions (not matching overlapping instances), use this method.

+5
Oct 09 '10 at 4:13
source share
 #An example for counting matched groups import re pattern = re.compile(r'(\w+).(\d+).(\w+).(\w+)', re.IGNORECASE) search_str = "My 11 Char String" res = re.match(pattern, search_str) print(len(res.groups())) # len = 4 print (res.group(1) ) #My print (res.group(2) ) #11 print (res.group(3) ) #Char print (res.group(4) ) #String 
+4
Sep 16 '16 at 5:06
source share

In those moments when you really want to avoid creating lists:

 import re import operator from functools import reduce count = reduce(operator.add, (1 for _ in re.finditer(my_pattern, my_string))) 

Sometimes you may need to work with huge strings. This can help.

+1
Nov 18 '17 at 3:25
source share

I know this is a bit outdated, but it is a concise function for counting regex patterns.

 def regex_cnt(string, pattern): return len(re.findall(pattern, string)) string = 'abc123' regex_cnt(string, '[0-9]') 
+1
Oct 12 '18 at 14:09
source share



All Articles