in reply to Re: combined into a single regex
in thread combined into a single regex
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: combined into a single regex
by ysth (Canon) on Dec 27, 2005 at 09:59 UTC | |
by doctor_moron (Scribe) on Dec 30, 2005 at 13:00 UTC |