Python Regex Sentence Finder-Want to Ignore "a.m."

Viewed 65

I am developing a regex to find sentences, and I would like to ignore abbreviations that cause the regex to terminate before the end of the sentence. For example, I want to ignore "a.m." so that it returns "At 9:00 a.m. the store opens." instead of "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

I get back the following when I run 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.'

What am I doing wrong?

1 Answers

You could use a negative lookbehind to check that after matching a dot, there is not a.m. before it.

[A-Z].*?\w[.?!](?<!\ba\.m\.)(?!\S)

Explanation

  • [A-Z] Match a char A-Z
  • .*? Match 0+ times any char except a newline as least as possible
  • \w[.?!] Match a word char followed by either . ? or !
  • (?<!\ba\.m\.) Negative lookbehind to assert that directly to the left is not a.m.
  • (?!\S) Assert a whitespace boundary to the right

Regex demo

Related