Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (16)
    As of 2015-07-07 22:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (93 votes), past polls