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

使用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.")

 

posted @ 2023-10-31 16:29  你说夕阳很美  阅读(66)  评论(0)    收藏  举报