我已经实现了正则表达式公式,可以从给定字符串中提取前两个大写单词。如果第二个单词没有大写字母,我希望能够只提取字符串中的第一个单词。
以下是一些例子:
s = 'Smith John went to ss for Jones.'
s = 'Jones, Greg went to 2b for Smith.'
s = 'Doe went to ss for Jones.'
基本上,我只是希望正则表达式输出以下内容:
'Smith John'
'Jones, Greg'
'Doe'
我当前的正则表达式公式如下,但它无法捕获Doe这个示例:
new = re.findall(r'([A-Z][\w-]*(?:\s+[A-Z][\w-]*)+)', s)