Trying to use a reasonably long regex and it comes down to this small section that doesn’t match how I’d expect it to.
>>> re.search(r'(foo)((?<==)bar)?', 'foo').groups()
('foo', None)
>>> re.search(r'(foo)((?<==)bar)?', 'foo=bar').groups()
('foo', None)
The first one is what I’m after, the second should be returning ('foo', 'bar').
I suspect I’m just misunderstanding how lookbehinds are meant to work, some some explanation would be great.
The look behind target is never included in the match – it’s supposed to serve as an anchor, but not actually be consumed by the regex.
The look behind pattern is only supposed to match if the current position is preceded by the target. In your case, after matching the “foo” in the string, the current position is at the “=”, which is not preceded by a “=” – it’s preceded by an “o”.
Another way to see this is by looking at the re documentation and reading
After you match the
foo, your look behind is trying to match at the beginning of (the remainder of) the string – this will never work.Others have suggested regexes that may probably serve you better, but I think you’re probably looking for
If you find the extra group is a little annoying, you could omit the inner “()”s and just chop the first character off the matched group…