I made a litle change youre regexp and tried to analyze

my @data = (-123.004,-.008,0,-0,.0987,1.1,12345,'d','test'); foreach my $value (@data){ if ( $value =~ /(^-?)(\d?|\d+)(\.?)(\d?|\d+)$/){ print "$1 - $2 - $3 - $4 true\n"; } else{ print "false\n"; } }

For this analyze i ignored the process of $1 - first grouping, i think i can understand that, so we straight to second grouping

For the $value = 123.004
1. Move on to second group and pick the first alternative (\d?)
2. "123" doesnt match (\d?), since (\d?) = match digit 1 or 0 times
3. Backtrack 1 character.
4. Pick the second alternative in the second group (\d+) or match digit 1 or more times, and it match, so we got "123" for $2
5. Move on to the third group, (\.?) match "." 1 or 0 times, so we got "." for $3
6. Move on to the fourth group, first alternative doesnt match so backtrack 1 character, try the second alternative and match "004", so we got "004" for $4.

for the $value = 12345
1. Move on to the second group and pick the first alternative (\d?)
2. "12345" doesnt match (\d?)
3. backtrack 1 character
4. Pick the second alternative in the second group (\d+) or match digit 1 or more times, and we got "1" for $2, i am not sure about this, i keep thinking that we should get "12345" for $2 (is there something in third grouping (\.?) ?)
5. Move on to the third group, "2345" doesnt match (\.?)
6. Move on to the fourth group, "2345" doesnt match (\d?), (\d?) only match "2" in "2345".
7. backtrack 1 character, try the second alternative (\d+) and "2345" match (\d+) or match digit 1 or more times.

sorry for my english, zak


In reply to Re^2: combined into a single regex by doctor_moron
in thread combined into a single regex by arcnon

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.