Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Don't ask to ask, just ask
 
PerlMonks  

Re: regex not working

by AnomalousMonk (Monsignor)
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!)


Comment on Re: regex not working
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://930106]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-04-19 13:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls