Perl Monk, Perl Meditation | |
PerlMonks |
Re: Regex and leading zeros in numbersby flyingmoose (Priest) |
on Mar 24, 2004 at 17:42 UTC ( [id://339488]=note: print w/replies, xml ) | Need Help?? |
Regarding the (\d)(\d) repitition you are using, I highly recommend you pick up a copy of "Learning Perl" and "Programming Perl" -- the O'Reilly Camel and Lllama books, as these explain regexes, and the fact tht you can do things like (\d{0..9}) or (\d*) to specifiy a certain number of digits.
It always helps to read a little documentation before charging too deeply into new things! :) While the 'don't use regexes' comments are 100% right, a regex cleanup solution could look something like $x=~s/[0]?(\d+)/$1/;. The question mark means 'optional', and the '+' means 'one or more of'. The $1 represents what you matched inside parens between the first set of slashes. So we are replacing digits with a leading zero with just the digits. Again, read the docs, they are excellent. (as usual, all code is untested)
In Section
Seekers of Perl Wisdom
|
|