python 获取re.search()匹配到的数量

发布时间 2023-10-31 16:36:18作者: 你说夕阳很美

使用groups()

import re

text = "Hello, my name is John Doe. I live in New York."
match = re.search(r'\b(\w+)\b', text)

if match:
    print("Match found: ", match.group())
    print("Number of groups: ", len(match.groups()))
else:
    print("No match found.")