Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: regex not working

by AnomalousMonk (Chancellor)
on Oct 07, 2011 at 05:14 UTC ( #930106=note: print w/replies, xml ) Need Help??

in reply to regex not working

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!)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://930106]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2017-12-18 09:13 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (475 votes). Check out past polls.