正则表达式 - 以特定的东西开始/结束,并在其间重复模式

时间:2017-12-10 16:07:14

标签: python regex string

我想编写一个使用以下语法匹配名称的正则表达式。

"Show log 8006, Conan O'Brian talks about Freddy Mercury, Evan.R Wood, Chan Kong-sang and McDonald?, Great People."

第1组:Conan O' Brian

第2组:Freddy Mercury

第3组:Evan.R Wood

第4组:Chan Kong-sang

第5组:麦当劳?

一致性是"显示日志????,'名称'谈论'姓名'和'姓名','主题'"。

名称数量可能会有所不同,至少有3个名称,没有最大名称。

1 个答案:

答案 0 :(得分:1)

Python很棒但是从正则表达式开始不会伤到自己 没有它们,您的任务实际上更容易实现:

string = """
Lorem ipsum 
Show log 8006, Conan O'Brian talks about Freddy Mercury, Evan.R Wood, Chan Kong-sang and McDonald?, Great People.
Lorem ipsum
"""

for line in string.split("\n"):
    if line.startswith('Show log'):
        parts = line.replace(' talks about ', ', ').replace(' and ', ', ').split(", ")
        print(parts[1:])

这会产生

["Conan O'Brian", 'Freddy Mercury', 'Evan.R Wood', 'Chan Kong-sang', 'McDonald?', 'Great People.']
相关问题