UFC Insider
UFC Insider

Reputation: 838

Why don't these regular expressions match the same?

Given the string 12312112211212123 I can't understand why the following regex: (1.+)\1 matches

But this one doesn't: ((1.+)\1)

What is the explanation for this?

Upvotes: 2

Views: 51

Answers (2)

Tomalak
Tomalak

Reputation: 338208

\1 refers to the first set of parentheses in the expression.

Think about it.

Upvotes: 2

Sebastian Proske
Sebastian Proske

Reputation: 8413

The second regex doesn't match, because the outer group is seen as group 1 and the inner group as group 2. To make it match, you would have to use ((1.+)\2).

Upvotes: 4

Related Questions