@Alan-Kilborn said in Why does this regexp fail to match?:
It’s so contrived it makes my head hurt. :-P
I suspect this is the non-contrived version (which feels much better):
I agree on the hurt, or at least spinning, head. In hindsight, I should have used ^date\K time which both works and is easier to mentally parse.
I now understand why my first expression failed in the original message on this thread.
FWIW, while parsing @Coises’ reply I found https://www.regular-expressions.info/lookaround.html via Google that has a decent explanation on why many regexp engines require fixed length or width lookbehinds. Some engines have ways around this restriction but each of those seems to create their own cans of worms.
For now I’ll stick with lookbehinds only as the first thing in an expression or use \K as I now see that if I have a lookbehind later in an expression, even after a zero length ^ match, that it can be a brain twister to figure out what is happening.
Here’s a simplified version of the contrived data and regexp that is easier to understand. The data is
abc
Searching using any of these get you the same results:
(?<=a)bc
b(?<=ab)c
bc(?<=abc)
The latter two end up inspecting the b or bc in the data twice.