s = 'A boy is playing and he is wearing shirt.'
我的正则表达式是 ((?:\S+\s+)\bis\b)
我的输出:['boy is' ,'he is']
预期输出:['boy','he']
请您参考如下方法:
您可以稍微重新组织您的捕获组以将 is 词保留在组之外并使用 re.findall:
>>> s = 'A boy is playing and he is wearing shirt.'
>>> re.findall(r'(\S+)\s+is\b', s)
['boy', 'he']
findall 仅返回捕获的组,如果您的正则表达式中有的话。
另请注意,在匹配空格后无需使用 \b(单词边界)。
