0

I am experimenting with regex backreferences and I am stuck at some very simple case.

Regexp which does not match the whole input: ([\D\s]+)(\d+)\1

Input: Foo 12 bar

Link: https://regex101.com/r/yfFLmc/1/

On the other hand, the same regex without backreference(but with repetition of first capturing group) matches the whole input correctly: ([\D\s]+)(\d+)([\D\s]+)

Why the regex with backreference doesn't match the whole input?

CuriousGuy
  • 1,376
  • 3
  • 16
  • 38
  • 1
    Backreferences match the same text matched with the group pattern, not the same pattern. Backreferences are not subroutine calls. Just check [this demo](https://regex101.com/r/yfFLmc/2) with a subroutine call. – Wiktor Stribiżew Jun 18 '19 at 20:08
  • Ahaa, I see. Thank you very much! I indeed had wrong understanding - I actually need the subroutine call. – CuriousGuy Jun 18 '19 at 20:15

0 Answers0