【发布时间】:2021-01-27 01:17:15
【问题描述】:
我正在开发一个正则表达式来查找句子,我想忽略导致正则表达式在句子结尾之前终止的缩写。例如,我想忽略“a.m.”,使其返回“At 9:00 a.m. the store opens.”而不是“At 9:00 a.m.”。 "
def sentence_finder(x):
RegexObject = re.compile(r'[A-Z].+?\b(?!a\.m\.\b)\w+[.?!](?!\S)')
Variable = RegexObject.findall(x)
return Variable
我在运行 pytest 时得到以下信息:
def test_pass_Ignore_am():
> assert DuplicateSentences.sentence_finder("At 9:00 a.m. the store opens.") == ["At 9:00 a.m. the store opens."]
E AssertionError: assert ['At 9:00 a.m.'] == ['At 9:00 a.m...store opens.']
E At index 0 diff: 'At 9:00 a.m.' != 'At 9:00 a.m. the store opens.'
我做错了什么?
【问题讨论】:
-
试试
[A-Z](?:a\.m\.|.)*?\w[.?!](?!\S),见regex101.com/r/78k0By/1 -
p.m.e.g.Ave.等呢?您应该使用更通用的规则,而不是仅排除a.m. -
对于之前关于句子查找器的工作,您应该查看我对Using regular expression as a tokenizer 的回答。它在 complex legal narrative 上完美运行。
-
试试
(?=[A-Z]).+?[.?!](?=\s*(?:[A-Z]|$)),它应该可以在大部分时间工作。