這是此問題的後續操作和複雜性:Extracting contents of a string within parentheses。使用正則表達式從字符串中提取信息
在這個問題,我有以下字符串 -
"Will Farrell (Nick Hasley), Rebecca Hall (Samantha)"
,我想獲得一個元組列表中的(actor, character)
形式 -
[('Will Farrell', 'Nick Hasley'), ('Rebecca Hall', 'Samantha')]
要概括的問題,我有一個稍微複雜的字符串,我需要提取相同的信息。我的字符串是 -
"Will Ferrell (Nick Halsey), Rebecca Hall (Samantha), Glenn Howerton (Gary),
with Stephen Root and Laura Dern (Delilah)"
我需要格式化這個如下:
[('Will Farrell', 'Nick Hasley'), ('Rebecca Hall', 'Samantha'), ('Glenn Howerton', 'Gary'),
('Stephen Root',''), ('Lauren Dern', 'Delilah')]
我知道我可以代替填充詞(與,和,&等),但可不知道如何添加空白條目 - ''
- 如果沒有演員的角色名稱(在本例中是Stephen Root)。這樣做最好的方法是什麼?
最後,我需要考慮一個角色是否有多個角色,併爲該角色擁有的每個角色構建一個元組。最終的字符串我是:
"Will Ferrell (Nick Halsey), Rebecca Hall (Samantha), Glenn Howerton (Gary, Brad), with
Stephen Root and Laura Dern (Delilah, Stacy)"
,我需要建立一個元組的列表如下:
[('Will Farrell', 'Nick Hasley'), ('Rebecca Hall', 'Samantha'), ('Glenn Howerton', 'Gary'),
('Glenn Howerton', 'Brad'), ('Stephen Root',''), ('Lauren Dern', 'Delilah'), ('Lauren Dern', 'Stacy')]
謝謝。
@邁克爾:感謝你的拼寫編輯。 – David542
使用正則表達式真的有必要嗎? – utdemir
不,它可以是任何東西。無論工作和最好。 – David542