[Solved] Purpose of the parentheses in the regex \b(\w+)(\W+\1\b)+ [closed]


The second parantheses are needed because the + operator is connected to it.

For example if you have a+ then it matches one up to infinite repetitions of the character a, like the word aaaa. If you use the + together with a group then the whole group is allowed to repeat, like in (ab)+ which matches words like ababab but not aaaabbb.

The first parantheses are used as a capturing group. Its results are used in the second part where you have \1 which matches the match of the first capturing group.

See this example at regex101.com which also explains the effects in detail: regex101.com/r/3aLdDh/1

1

solved Purpose of the parentheses in the regex \b(\w+)(\W+\1\b)+ [closed]