Reuse-patterns-using-capture-groups specify number of patterns

Reuse-patterns-using-capture-groups specify number of patterns
0

#23

Different entries in the same array.
/(\d+)(\s)\1\2/ would be the same as saying /\d+\s\d+\s/ because the \1 repeats the first capture group and \2 repeats the second one.


#24

You could use the + operator on your \d the same as {1,} as they both find 1 or more occurrences of the preceding term.

Verbally, your regex is looking for a string that starts with 1+ digits followed by a space followed by 1+ digits (the \1 is repeating what’s in the first capture group) followed by a space followed by 1+ digits that ends the substring. Thus anything with less than two number groupings or more than three wouldn’t qualify. Hope that helps!


#25

for some reason when I use \d+ it does not pass the test whatsoever. I guess some website bug or something. I tried that at first and then just started trying all kind of staff and d{1,} worked


#26

let repeatNum = “42 42 42”;
let reRegex = /^(\d+)\s\1\s\1$/; // Change this line
let result = reRegex.test(repeatNum);


#27

Hi,
can you explain, why the ^ at the beginning and the $ at the end?

Thanks! :bowing_man:


#28

^ for start of the string and $ for end of the string. in both case we want the same word.


#29

I just want to make sure I understand this correctly, the reason this code /^(\d+)\s\1\s\1$/; works and not /(\d+)\s\1\s\1/; is because by using the carrot and dollar sign is forcing the computer that the last digit \1$ HAS to be placed after the first digit at the beginning ^(\d), space, digit and space \s\1\s; whereas the other code /(\d+)\s\1\s\1/; is allowing it to repeat for some reason?


#30

I played around with it a little bit and the only way I got it to work with different numbers in front or behind was with a look forward and look behind. It looks kinda funky but here it is: /(\d+)\s\1\s\1(?<!\1\s\1\s\1\s\1)(?!\s\1)/. it will work where three numbers are the same in a row but not 4, and will also pull multiple sets of three out of a string. unfortunately it doesn’t work the the “Your regex should reuse the capture group twice.” and “Your regex should have two spaces separating the three numbers.” requirements of the challenge (even though it does do those things, just with more added on to the end).


#31

I would like think my Regex is right and I tried online regex editor it worked for every string in the challenge.
it passed every string including ‘42 42 42 42’ but not ‘42 42 42’. Here is my regex: /^\b(\d+)\s\1\s\1\b$/g


#32

Hi Alimama, if you take out the g flag of your regex it will pass the test.


#33

Solution that passed the test for me:

/^(\d+)(\s)\1\s\1$/;


#34

why does this code work exactly?


#35

Your code is not working to me.

I got it and fixed to this … :

let reRegex = /^(\d+)\s\1\s\1$/;


#36

Your code worked for me as well–thanks for your help!

I wonder if you could help to explain why the following would not work:
/^(\d+)\s\3/;

I think it’s because the above snippet would render true for number space numbernumbernumber. So, 42 424242 would work with that code, but 42 42 42 would not.

Conversely, using /^(\d+)\s\1\s\1$/ means that we have to start with a number, then a space, then the first number repeated, then another space, then end with the same number repeated. Hence, 42 42 42 works.

Am I getting this right?


#37

\3 refers to the third capturing group. No such group exists. In the regular expression /(a)(b)(c)/

  • \1 refers to “a”
  • \2 refers to “b”
  • \3 refers to “c”

#38

Thanks, that makes sense–so /( a )( b )( c )\1\2\3/ would then render true for abcabc.

Is there a RegEx expression to search for multiple repeats of the same capturing group? Or do you have to give the \1 every time you want to see ( a ) repeated?


#39

I dont understand how this regEx work. I tried on RegExr.com and it does not work at all


#40

Try it here: https://regex101.com/
It’s explained as follows-

^ asserts position at start of the string
1st Capturing Group (\d+)
\d+ matches a digit (equal to [0-9])

  • Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
    \s matches any whitespace character (equal to [\r\n\t\f\v ])
    \1 matches the same text as most recently matched by the 1st capturing group
    \s matches any whitespace character (equal to [\r\n\t\f\v ])
    \1 matches the same text as most recently matched by the 1st capturing group
    $ asserts position at the end of the string

I was just struggling with this, and I found the explanation pretty clear.
I think the key regarding that solution are the ^ and $.
I’ve tried to use {3} with no prevail.


#41

I understand all that clearly. I just did not understand why the regEx online tool did not found any matches using that expression above. Now I understand that the \1 is like a $1 a variable placement there. Before that. I was confused. FCC does not explain things very clear.


#42

Hmm… perhaps I’m wrong, (I’ll be corrected if I am) but is actually not a variable?
The \1 is the second use of the capture group, and the $ is indicating the end of the string. Not sure is technically a variable.

Regarding your last statement- sometimes, yes.

I think that the FCC aim is always to do you yourself a deep research before coming here. Which I’m sure you did.
I got frustrated before because of how much time it took me to solve somethings, or not being able to solve them at all. But no more.
Specially something like a regex. I mean, is fine to understand the core, but most of the developers just learn to google the needed regex. I see no point in trying to learn every possible pattern. Yes to understand the underlying logic. Otherwise, you won’t be able to reach the solution at all.