Determining Number of Groups Returned From Regex Search in Python

Question:

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?

Asked By: pHorseSpec

||

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)

Answered By: Bharel
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.