I want to match After if there isn't an -ing word after it (and before a comma). So there shouldn't be an -ing word between After and the comma.
Desired match (bold):
After sitting down, he began to talk.
After finally sitting down, he began to talk.
After he sat down, he began to talk.
I thought this regex would do it:
\bAfter\b.*(?!\w ing) ,
But it's also matching After if there isn't an -ing word after it:
After sitting down, he began to talk.
After finally sitting down, he began to talk.
After he sat down, he began to talk.
Why is this and how to fix it?
CodePudding user response:
Try this regex
Matches only sentence from After and a comma, where there's no word with -ing after the wo
Just a lazy quantifier to the . (which instead of \w in your regex) does the trick
\bAfter (?!. ?ing).*?,
(And also a lazy quantifier after the second .*, just in case if there's 2 commas in the same sentence)
Output:

Tell me if its not working for you...
CodePudding user response:
The pattern you tried:
- Using
.*will first match the whole line, then it will backtrack to match the first encountered comma, so it will also match all comma's in between. - It can match the first encountered comma because this part
(?!\w ing),asserts that from the current position there are no word chars followed byingand then match a,But\wdoes not match a comma so the asserting will always be true.
You can exclude matching a comma in the negative lookahead using a negated character class, and then also match until the first occurrence of a comma afterwards.
\bAfter\b(?![^,\n]*ing)[^,\n]*,
In parts, the pattern matches:
\bAfter\bMatch the wordAfterbetween word boundaries(?![^,\n]*ing)Negative lookahead to assert that from the current position there is no occurrence ofingwithout crossing a comma[^,\n]*,Match optional chars except a comma (or a newline if you don't want to cross lines)
