0

/ab(?=.{1})$/g doesn't match "abdabd" or anything else

It's the anchor $ that is troubling me. What can this regex match ?

airnet
  • 2,565
  • 5
  • 31
  • 35
  • 1
    This regex cannot match anything. It's trying to look ahead into the `$` anchor. – AKHolland Jul 28 '15 at 17:24
  • The `(?=.{1})` expects to find a character at the end, but you won't let it because you put the EOS at that spot. Teach a man to fish .. –  Jul 28 '15 at 17:26
  • 1
    oh ok, it's like asking to find two different things at the same time. Like "ab" followed by a single character and "ab" followed by EOS ? – airnet Jul 28 '15 at 17:29
  • @airmet did you want explanation or solution. – Avinash Raj Jul 28 '15 at 17:29
  • @airnet Yes, and the order doesn't matter: `/ab$(?=.)/` means the same thing. – melpomene Jul 28 '15 at 17:30
  • @Borodin did you notice that op also want to modify his regex to find a match? – Avinash Raj Jul 28 '15 at 17:30
  • Another way to explain this: lookarounds don’t consume characters. `(?=.{1})$` doesn’t mean “match one character and then match the EOS”, it means “expect one character, but definitely match the end of string”, which is a contradiction. – Sebastian Simon Aug 16 '16 at 05:13

1 Answers1

5

What can this regex match ?

This regex won't match anything and is guaranteed to fail because:

ab       - will literally match ab
(?=.{1}) - will use lookup to make sure there is at least 1 character after ab
$        - will assert end of input after ab

both conditions can never be met hence your regex will always fail.

anubhava
  • 761,203
  • 64
  • 569
  • 643