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?
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)
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?
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)