.
matches if it is not "w" in "word1" or "word2" (can be simplified \bword1\b|\bword2\b
→ \bword[12]\b
), between non-words. This is the meaning of the negative assertion,
+?
means at least one such .
,
- but actually only one, because the quantifier is non-greedy and is followed by
\s*
that always matches. Therefore+?
can be dropped,
\s*?
in this assertion is meaningless, as it always matches, and consumes no input, and not followed by anything,
- The positive lookahead assertion
(?=...)
here means that the position is followed by any character (except for "w" "word", etc. as is described above).
Further simplifications would remove group captures, which could be required in the context.
So, the simplified regex is (?=((?!\bword[12]\b).))
. It will succeed before any character of the input, except at the beginning of "word1" or "word2" between non-words. The match will be empty, but the first capture group will contain the following character.
https://regex101.com/r/O10c3u/1