Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining Number of Groups Returned From Regex Search in Python

I'm performing a regex search in python like the one below:

import re
regexSearch = re.search(r'FTP-exception-sources-\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', line, re.M|re.I )
if regexSearch:
        outputFile2.write(str(lineCounter) + " , " + regexSearch.group(0) + "\n")

How can I determine the number of groups that get returned from the regex search?

like image 831
pHorseSpec Avatar asked Oct 17 '25 19:10

pHorseSpec


1 Answers

regexSearch.groups() is all of the groups. len(regexSearch.groups()) gets the count.
In your case there will always be 0 groups as your regex does not contain groups (group(0) is the whole match and not really a group)

like image 181
Bharel Avatar answered Oct 20 '25 08:10

Bharel