Perl Monk, Perl Meditation | |
PerlMonks |
Re: regex to capture an unsigned decimal value, but also preserving the user's formatting.by davido (Cardinal) |
on May 04, 2016 at 05:57 UTC ( [id://1162143]=note: print w/replies, xml ) | Need Help?? |
You can make the dot possessive and the trailing digits greedy but optional:
This yields the following output:
I do not know what additional edge cases you might encounter that would break this. But for your test cases it works. Two tricks here. First, it was a mistake to use the + quantifier on the trailing \d, because then it became a required portion of the match. If it's required, then 12. is going to fail to match the decimal point because it comes before the trailing \d+. By making the quantifier * we stay greedy, but optional. The next trick is the ?+ quantifier for the decimal point. The + here signifies to be possessive -- once it has matched, hold onto what it matched against, and don't give it up during backtracking. For reference, here are the two regexes (yours and mine) in close proximity. You can disregard the parens here; I'm only using them for capturing, which you were achieving by referring to the match hash.
I suggest walking through your original regular expression, and the one I've provided using the Regexp::Debugger's rxrx utility. I think my inadequate description will be clearer once you see the wheels in motion. Update: I've fixed the backslashing of the . in the pattern. Dave
In Section
Seekers of Perl Wisdom
|
|