我有以下模式和字符串reg-ex匹配问题。模式基本上是一个名称,后面跟着任意数量的字符,后跟一个短语(见下面的模式),后面跟着任意数量的字符,后跟机构名称。python reg-ex模式不匹配
pattern = "[David Maxwell|David|Maxwell] .* [educated at|graduated from|attended|studied at|graduate of] .* Eton College"
str = "David Maxwell was educated at Eton College, where he was a King's Scholar and Captain of Boats, and at Cambridge University where he rowed in the winning Cambridge boat in the 1971 and 1972 Boat Races."
match = re.search(pattern, str)
但是搜索方法返回一个不匹配上面的str?我的reg-ex是否正确?我是reg-ex的新手。任何帮助表示赞赏
你想用(... | ...),而不是[... | ...] – sshashank124
此外,在蟒蛇,它是最好的定义正则表达式模式时要使用原始字符串。 – bgm387
我改变了它。但似乎还有另一个问题。如果我将“受过教育”改为“受过教育”,它就会匹配。任何想法,为什么? – raghu