python - 正则表达式帮助将列表拆分为二元组

标签 python regex

给定 Actor 列表,他们的角色名称放在括号中,用分号(;)或逗号(,)分隔:

Shelley Winters [Ruby]; Millicent Martin [Siddie]; Julia Foster [Gilda]; 
Jane Asher [Annie]; Shirley Ann Field [Carla]; Vivien Merchant [Lily]; 
Eleanor Bron [Woman Doctor], Denholm Elliott [Mr. Smith; abortionist]; 
Alfie Bass [Harry]

我如何将其解析为 [( Actor ,角色),...] 形式的两种类型列表

--> [('Shelley Winters', 'Ruby'), ('Millicent Martin', 'Siddie'), 
     ('Denholm Elliott', 'Mr. Smith; abortionist')]

我原来有:

actors = [item.strip().rstrip(']') for item in re.split('\[|,|;',data['actors'])]
data['actors'] = [(actors[i], actors[i + 1]) for i in range(0, len(actors), 2)]

但这不太有效,因为它还会将括号内的项目分开。

最佳答案

你可以选择类似的东西:

>>> re.findall(r'(\w[\w\s\.]+?)\s*\[([\w\s;\.,]+)\][,;\s$]*', s)
[('Shelley Winters', 'Ruby'),
 ('Millicent Martin', 'Siddie'),
 ('Julia Foster', 'Gilda'),
 ('Jane Asher', 'Annie'),
 ('Shirley Ann Field', 'Carla'),
 ('Vivien Merchant', 'Lily'),
 ('Eleanor Bron', 'Woman Doctor'),
 ('Denholm Elliott', 'Mr. Smith; abortionist'),
 ('Alfie Bass', 'Harry')]

还可以使用 .*? 来简化一些事情:

re.findall(r'(\w.*?)\s*\[(.*?)\][,;\s$]*', s)

关于python - 正则表达式帮助将列表拆分为二元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14904099/

相关文章:

python - Anaconda 未能通过 md5 检查更新?

c++ boost正则表达式哪个元素为真

java - 正则表达式模式需要帮助 Java toString() 方法

java - 正则表达式从文本中每个单词的末尾删除点

java - 使用找到的模式从 Java 正则表达式匹配器获取 NULL 值

java - 构建助手-maven-插件目标 :regex-property how to access generated property

python - 装箱 Pandas value_counts

python - 将 lambda 表达式转换为函数以更好地理解它

python - Flask-Babel 的 Flask 测试设置

python - 在 Python 中,如何将警告视为异常?