Let's look at the /[$i]{$_}/ regex. The [$i] defines a character class that matches whatever $i interpolates as a string into the regex. In this case, $i never changes: it is always the number 0 which interpolates as the '0' (zero) character, so the character class matches a '0' character and nothing else.
The {$_} quantifier of the [0] character class (which is the same as '0') takes on the values that $_ interpolates into the regex on each iteration of the for-loop. These values are from the @nums array and happen to be (2, 0, 2, 0) successively.
So the [$i]{$_} regex expression alternates between 0{2} and 0{0}. That's all there is to the regex. The 0{2} regex does not match (twice!) against the $num string of '2020' because that string does not have two consecutive '0' characters anywhere in it. The 0{0} regex does match (again, twice) because there is a place in the '2020' string where there is no '0' character.
See perlre, perlretut, perlrequick.
Update: Rats. Missed the $i++; statement. Of course, the analysis of jwkrahn is correct, while mine is wandering up and down in the street outside the ballpark. .oO(But was $i++; present in the original original post, the one to which I replied? The more I think about it, the more I think it wasn't!)
In reply to Re: regex not working
by AnomalousMonk
in thread regex not working
by andrewbriggs
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |