Basically, I want to do this:
Text and text text. Text text. And text. (Don't match this "And.")
Text and text text. And text. (Match this "And.")
Right now, I have this regex: .*\b(and)\b.*\. And
. But it matches both And
's in the example above: https://regexr.com/65k52
How to modify this regex so it only matches And
if the previous sentence has and
?
If you want to use .
+ whitespace as a sentence separator sequence you can use
(?=\S)[^.]*(?:\.(?!\s)[^.]*)*?\band\b[^.]*(?:\.(?!\s)[^.]*)*\.\s+And\b
(?=\S)(?:[^.]|\.(?!\s))*?\band\b(?:[^.]|\.(?!\s))*\.\s+And\b
See the regex demo #1 and regex demo #2. Details:
(?=\S)
- next char must be a non-whitespace(?:[^.]|\.(?!\s))*?
- zero or more (but as few as possible) occurrences of any one char other than .
or a .
that is not immediately followed with whitespace\band\b
- whole word and
(?:[^.]|\.(?!\s))*
- zero or more (but as many as possible) occurrences of any one char other than .
or a .
that is not immediately followed with whitespace\.
- a dot\s+
- one or more whitespacesAnd\b
- a whole word And
.