-1

I am currently working on Regex Challenges on Hackerrank and can't find resources on to why using capture group references are preferred over repetitions. Here is the link to the Challenge: https://www.hackerrank.com/contests/chingu-challenge-8-regex/challenges/matching-same-text-again-again/problem

Why is the first option of the following two preferred over the second? (Meaning what is the difference?! The second one gives me a mistake on the challenge)

1.Ref. Capture Group: ^([a-z]\w\s\W\d\D[A-Z][A-Za-z][aeiouAEIOU]\S)\1$
2.Repetition:         ^([a-z]\w\s\W\d\D[A-Z][A-Za-z][aeiouAEIOU]\S){2}$ 

Thanks in advance.

1 Answers1

0

The first one matches something that matches the group pattern followed by the same thing again.

The first one matches something that matches the group pattern followed by something that matches the group pattern - it doesn't have to be the same thing again. After all, you don't expect [a-z]{2} to only match aa, bb etc., do you?

Rawling
  • 45,907
  • 6
  • 80
  • 115