3 回答

TA貢獻1848條經驗 獲得超2個贊
讓我們將答案分解為兩個簡單的步驟。
將整個字符串轉換為一組情侶姓名。
獲取所有與所請求的模式匹配的對。
我們對遵循以下模式的情侶名字感興趣:
<Name1> and <Name2> <Last-name> <May-or-may-not-be-words-separated-by-spaces>.
<Name1> and <Name2> <Last-name>
但我們只對每個匹配字符串的部分感興趣。現在我們已經定義了我們想要做什么,下面是相同的代碼。
import re
testStr = """Rob and Amber Mariano, Heather Robinson,
Jane and John Smith, Kiwan and Nichols Brady John,
Jimmy Nichols, Melanie Carbone, Jim Green and Nancy Brown,
Todd and Sana Clegg with Tatiana Perkin
"""
# Pattern definition for the match
regExpr = re.compile("^(\w+\sand\s\w+\s\w+)(\s\w)*")
# Remove whitespaces introduced at the beginning due to splitting
coupleList = [s.strip() for s in testStr.split(',')]
# Find all strings that have a matching string, for rest match() returns None
matchedList = [regExpr.match(s) for s in coupleList]
# Select first group which extracts the necessary pattern from every matched string
result = [s.group(1) for s in matchedList if s is not None ]

TA貢獻1804條經驗 獲得超2個贊
有點晚了,但可能是最簡單的正則表達式
import re
regex = r"(?:, |^)(\w+\sand\s\w+\s\w+)"
test_str = "Rob and Amber Mariano, Heather Robinson, Jane and John Smith, Kiwan and Nichols Brady, John, Jimmy Nichols, Melanie Carbone, Jim Green and Nancy Brown, Todd and Sana Clegg with Tatiana Perkin"
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print (match.group(groupNum))
輸出:
Rob and Amber Mariano
Jane and John Smith
Kiwan and Nichols Brady
Todd and Sana Clegg

TA貢獻1851條經驗 獲得超3個贊
試試這個...按預期完美工作
(,\s|^)([A-Z][a-z]+\sand\s[A-Z][a-z]+(\s[A-Z][a-z]+)+)
測試腳本:
import re
a=re.findall("(,\s|^)([A-Z][a-z]+\sand\s[A-Z][a-z]+(\s[A-Z][a-z]+)+)","Rob and Amber Mariano, Heather Robinson, Jane and John Smith, Kiwan and Nichols Brady John, Jimmy Nichols, Melanie Carbone, Jim Green and Nancy Brown, Todd and Sana Clegg with Tatiana Perkin")
print(a)
回復:
[('', 'Rob and Amber Mariano', ' Mariano'), (', ', 'Jane and John Smith', ' Smith'), (', ', 'Kiwan and Nichols Brady John', ' John'), (', ', 'Todd and Sana Clegg', ' Clegg')]
添加回答
舉報