(Note: not a duplicate of Why can't you use repetition quantifiers in zero-width look behind assertions; see end of post.)
I'm trying to write a grep -P
(Perl) regex that matches B, when it is not preceded by A -- regardless of whether there is intervening whitespace.
So, I tried this negative lookbehind, and tested it in regex101.com:
(?<!A)\s*B
This causes "AB" not to be matched, which is good, but "A B" does result in a match, which is not what I want.
I am not exactly sure why this is. It has something to do with the fact that \s* matches the empty string "", and you can say that there are, as such, infinity matches of \s* between A and B. But why does this affect "A B" but not "AB"?
Is the following regex a proper solution, and if so, why exactly does it fix the problem?
(?<![A\s])\s*B
I posted this before and it was incorrectly marked as a duplicate question. The variable-length thing I'm looking for is part of the match, not part of the negative lookbehind itself -- so this quite different from the other question. Yes, I could put the \s* inside the negative lookbehind, but I haven't done so (and doing so is not supported, as the other question explains). Also, I am particularly interested in why the alternate regex I post above works, since I know it works but I'm not exactly sure why. The other question did not help answer that.