Do you want something as simple as: /^(-?)(\d+)(\.?)(\d*)$/
That's true, i agree, i am just curious of what perl does when it tries to match the regexp :
(^-?)(\d?|\d+)(\.?)(\d?|\d+)$/ #on -123.004
Because of my poor English, i am afraid i just misinterpreted your explanation (again).
So i asked for help to ID-PERL and i found the answers from Jacinta and pope (pope introduced re 'debug', i just need more time to understand about this).
I saved the conversation between me and jarich in my pad for my own note.
Here we go,
(^-?)(\d?|\d+)(\.?)(\d?|\d+)$/ #on -123.004
STEP BY STEP ANALYSIS
1. Match "-" with (^-?) so we set $1 to -
2 Move on to 2nd group, and pick the 1st alternative and i think its \d (NOT \d?). \d will then match the digit 1. In other words, i can say that :
\d? : we try to match a single digit, if we can't, we'll try to match no digits
3. Move on to the 3rd group, and pick the 1st alternative,\.?, try to match a dot, fail to find one, so choose the zero dots option. So \3 is undefined.
4. Move on to the 4th group,first check it with \d, we match 2 with \d, move on to the next requirement which then says we must be at the end of string. since we're *not* at the end of the string we have to use a different alternative. We have 2 options remaining before further backtracking :
- try matching *no* digits
- try matching one or more digits
5. We are not yet going to 2nd alternative, we're going to try \d?, we match \d? with 2, and too bad 2 is not at the end of the string, so 2 doesnt match with 1st alternative in the 4th group.
6. Now we're going to 2nd alternative in 4th group, \d+, match 2 followed by 3, too bad its still not at the end of the string. So we're going to back to step 2, or to the 2nd alternative in 2nd group.
Strictly speaking, this is the 3rd alternative that is tried, although the second part of the alternation
7. Move on to 2nd group, try the 2nd alternative, \d+, match "1" followed by "2" followed by "3", set $2 to 123
Actually first it'll try matching no digits, then the optional dot, then (\d?|+d) then find it can't get to the end of the string... after a while it'll come back and try the \d+ at the start and finally get somewhere.
So we set $2 to 123, i'll stop here, about $3 and $4 i think it's easier to understand.
Thanks, zak
|