What does \1, \2 etc refers to in regular expression?
\1 refers to the first capturing group.
\2 refers to the second capturing group.
\2 refers to the third capturing group.
. . .
Let’s understand more clearly with an example.
Suppose we have a strong
Hello World 123 Hello World Hello 123
The regular expression to match this string is :
(Hello)\s(World)\s(123)\s\1\s\2\s\1\s\3
Explanation :
(Hello) : Hello inside these two parentheses acts as a group.
\s : Represents space here.
(World) : World inside these two parentheses acts as a group.
(123) : 123 inside these two parentheses acts as a group.
\1 : Refers 1st capturing group which is Hello here
\2 : Refers 2nd capturing group which is World here
\3 : Refers 3rd capturing group which is 123 here
Remember: Here \1,\2 or \3 is the reference of capturing group but does not act as capturing group. That means in the regular expression (Hello)\s(World)\s(123)\s\1\s\2\s\1\s\3 , the highlighted \1 and \2 will not be 4th and 5th capturing groups.