I was playing around with regular expression look-aheads and came across something I don't understand.
I would expect this regular expression:
(?=1)x
to match this string:
"x1"
But it doesn't. In ruby the code looks like:
> "x1".match /(?=1)x/
=> nil
Here's what I would expect to happen:
- We start with the regular expression parser's cursor on "x".
- The regexp engine searches the string for "1" and gets a match. The cursor is still on "x"
- The regexp engine searches for "x" and finds it, since the cursor hasn't moved.
- Success! Profit!
But I'm apparently mistaken, because it's not matching. Could someone please tell me where I've gone wrong?
Incidentally, I've noticed that if the pattern matched by the lookahead contains the characters I'm matching in the subsequent expression, it works. ie. (?=x)x
matches x1
just fine. I suspect this is the key to the mystery, but I'm just not getting it. :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…