Can someone elaborate on the example from BackReferences: Using Capture Groups in a RegEx Pattern exercise?

I really want to know more about the example from this exercise.

What’s the logic when we have for ex. three captured groups? What would be the order of the backlashes? Are they going backwards? Is the starting point between the last captured group and the first backlash?

I would highly recommend that you start testing out your own questions now. It’s fairly easy to cross-check in such cases what will happen if you tweaked things yourself.

But, the text after the image does clarify this for you -

The regular expression above will match the text HelloGoodbyeGoodbyeHello

The regex is matching the above text. Which indicates that adding \2 means that it will match the 2nd group, and then \1 will match the first group. So there is no inherent order here on the placement of these \number.

It’s what you want to match that matters.

If you wanted to match hibyehihi then you could have the expression (hi)(bye)\1\1. Something that can also be easily verified through code, so I recommend doing that and experimenting.


I was overwhelmed by the bizarreness of regex. I do my own tests now and I will check this issue mentioned here too.

Thank you for your reply! :slight_smile:

btw. I found another great regex tester: