Number of regex matches

前端 未结 6 1879
长情又很酷
长情又很酷 2020-12-05 03:49

I\'m using the finditer function in the re module to match some things and everything is working.

Now I need to find out how many matches

6条回答
  •  日久生厌
    2020-12-05 04:18

    I know this is a little old, but this but here 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]')
    

提交回复
热议问题