user1403588
user1403588

Reputation: 804

How do I match sentence punctuations with exceptions?

I am trying everything, but I just can't figure it out how to write correct regex.

For example I have a few sentences:

I want a car, bike etc. or watching football. Bring me money!<br><br>

Then I have exceptions:

etc\.
imho\.

And separators:

\.
\!

And I need to match those separators, but exclude exceptions. In sentences written above I need to match the end of the sentence (dot at the end and ! at the end), but I should not match etc.

I want a car, bike etc. or watching football. Bring me money!
-> Should be 2 sentences:
I want a car, bike etc. or watching football
Bring me money

Now I am using only firstseparator|secondseparator|... as regex so my result is
-> 3 sentences:
I want a car, bike etc
or watching football
Bring me money!

Upvotes: 0

Views: 57

Answers (1)

Santiago Palladino
Santiago Palladino

Reputation: 3542

You can use negative lookbehind to ensure you match . as long it is not preceded by an exception word. Try (?<!etc)\.

Upvotes: 1

Related Questions