简体   繁体   English

确定从Python中的正则表达式搜索返回的组数

[英]Determining Number of Groups Returned From Regex Search in Python

I'm performing a regex search in python like the one below: 我在python中执行正则表达式搜索,如下所示:

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? 如何确定从正则表达式搜索返回的groups数?

regexSearch.groups() is all of the groups. regexSearch.groups()是所有组。 len(regexSearch.groups()) gets the count. len(regexSearch.groups())得到计数。
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) 在你的情况下总会有0组,因为你的正则表达式不包含组( group(0)是整个匹配而不是真正的组)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM