在Python中匹配“类似名称”的字符串

时间:2019-03-25 16:19:18

标签: python regex python-3.x findall

我正在尝试写一个正则表达式,以匹配适当的大写名词,例如“ Oreo”,“ Snickers Bar”,“ McFlurry”。

import re

text = "George Washington, known as the \"Father of His Country,\" was an American soldier and statesman who served from 1789 to 1797 as the first President of the United States. He was commander-in-chief of the Continental Army during the American Revolutionary War and presided over the 1787 Constitutional Convention. As one of the leading Patriots, he was among the nation's Founding Fathers. Yankee Hotel Foxtrot Yankee Hotel Foxtrot."
reg = "[A-Z]\w+(\s*[A-Z]\w+)*"

re.findall(reg, text)

将输出提供给我

[' Washington', '', ' Country', '', '', ' States', '', ' Army', ' War', ' Convention', '', '', ' Fathers', ' Foxtrot']

显然,我要寻找的匹配项类型减去第一个单词。知道为什么我的正则表达式搜索似乎在一开始就验证了[A-Z]\w+却没有将其作为结果的一部分吗?

编辑:我应该补充一点,此表达式在pythex.org之类的正则表达式测试网站上正常工作,但在我的Google Colab笔记本中如上所述。

0 个答案:

没有答案