0

私はすべての英語の名前を検索するために以下のようなPythonコードを持っています:

a = "Bonds met Susann ("Sun") Margreth Branco, the mother of his first two children, in {{city-state|Montreal|Quebec}} in August 1987. They eloped in {{city-state|Las Vegas|Nevada}} Barry Bonds"

re.findall("(?:[A-Z][a-z'.]+\s*){1,4}",a)

私はそれを返したい:

['Bonds', 'Susann ("Sun") Margreth Branco', 'Montreal', 'Quebec', 'August', 'They', 'Las Vegas','Nevada','Barry Bonds']

私のコードは私が望むものを得ることができません、私の目標を達成するために正規表現を変更する方法は?

また、別の正規表現を使用したことを付け加えたいと思い(?:(([A-Z][a-z'.]+)|(\(&quot.*"\)))\s*){1,4}ます。regexpal.comでテストすると、そのテストWebサイトで必要なものが見つかりますが、Pythonでは、必要なものが返されませんがSusan、と、("Sun") MargrethおよびBranco、3つが別々に返されますがSusan ("Sun") Margreth Branco、結果には必要です。

4

1 に答える 1

1

おっしゃるように、「&quto」が付いた文字列も区切り文字のように見えました。

re.findall("[A-Z][a-z]*(?:(?:\\S*&quot\\S*|\\s)+[A-Z][a-z]*){0,3}", "Bonds met Susann ("Sun") Margreth Branco, the mother of his first two children, in {{city-state|Montreal|Quebec}} in August 1987. They eloped in {{city-state|Las Vegas|Nevada}} Barry Bonds")

出力:

['Bonds', 'Susann ("Sun") Margreth Branco', 'Montreal', 'Quebec', 'August', 'They', 'Las Vegas', 'Nevada', 'Barry Bonds']
于 2012-05-24T03:44:08.743 に答える