0

I'm trying to write a 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

Note that the variable-length thing I'm looking for is part of the match, not part of the negative lookbehind itself -- so this is not a duplicate of the other question.

std_answ
  • 1,039
  • 1
  • 11
  • 17
  • Are you going to use lookbehind in `grep`? – anubhava Mar 29 '17 at 15:54
  • You need to put `\s*` inside the negative lookbehind. But quantifiers aren't allowed inside lookbehinds. – Barmar Mar 29 '17 at 16:00
  • Yes, I will be using this in grep, using Perl regexes. Aka `grep -P` – std_answ Mar 29 '17 at 17:34
  • Why do I need to put the \s* inside the negative lookbehind? I have no problems with there being whitespace as part of the match. Note that this isn't a duplicate of the other question, since the variable length thing I'm looking for is **part of the match**, not part of the negative lookbehind. – std_answ Mar 29 '17 at 17:36

0 Answers0