3 回答
TA贡献2011条经验 获得超2个赞
您可以使用:
re.findall(r'((?:[A-Z]\w{2,}\s*){2,4})', line)
它可能会添加一个尾随空格,可以用 .strip()
TA贡献1786条经验 获得超11个赞
非正则表达式解决方案:
from string import punctuation as punc
def solve(strs):
words = [[]]
for i,x in enumerate(strs.split()):
x = x.strip(punc)
if x[0].isupper() and len(x)>2:
if words[-1] and words[-1][-1][0] == i-1:
words[-1].append((i,x))
else:
words.append([(i,x)])
names = [" ".join(y[1] for y in x) for x in words if 2 <= len(x) <= 4]
return ", ".join(names) if names else None
data = [
'Her name is Emily.',
'I work for Surya Soft.',
'I sent an email for Ery Wulandari.',
'Welcome to the Link Building Partner abc Fooo Foo program!'
]
for x in data:
print solve(x)
输出:
None
Surya Soft
Ery Wulandari
Link Building Partner, Fooo Foo
添加回答
举报