Reuse Patterns Using Capture Groupz

Here is my attempt at explaining this. Your code will match “42 42 42 42” because it meets the criteria you included in your RegEx. It will see that the second, third, and fourth part of the string matches your RegEx: “42 42 42 42

(/d+) --> will be the second 42,
(\s) --> will be the space after the second 42
\1 --> will be the third 42
\2 --> will be the space after the third 42
\1 --> will match the fourth (last) 42

Hence, your code fails because it matches the input with 4 numbers. Your code will give the same result if you provided it a string with “100 100 100 100”.

A hint, remember you can use ^ to state what we want at the beginning of a match, and $ to state what we want at the end. With that in hand, then you just focus on what should be in between.

Best of luck. And I hope this was helpful and makes sense.

38 Likes