Number of regex matches

前端 未结 6 1874
长情又很酷
长情又很酷 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条回答
  •  萌比男神i
    2020-12-05 04:28

    #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
    

提交回复
热议问题