Home > Software engineering >  How to prevent the following regex from matching ellipses?
How to prevent the following regex from matching ellipses?

Time:03-16

I'm using this regex:

(?<=[.?!])\.

To match the period that comes after the final punctuation in a sentence. Example:

Sentence.. (The second period will be matched.)

Sentence sentence?. (The period will be matched.)

Sentence sentence!. (The period will be matched.)

It's been working fine so far ... except for ellipsis:

Sentence sentence... (The last two periods will be matched.)

How to modify this regex so it doesn't match ellipsis ...?

Live regex: https://regexr.com/6hf3n

CodePudding user response:

As you already make use of a lookbehind, you might use:

(?<=[?!]|(?<!\.)\.(?!\.\.))\.

The pattern matches:

  • (?<= Positive lookbehind, assert what is to the left is
    • [?!] Match either ? or !
    • | Or
    • (?<!\.)\.(?!\.\.) A dot when there is no dot to the left, and not 2 dot to the right
  • ) Close lookbehind
  • \. Match a dot

See a regex demo.

  • Related