|
Posted by Marc Bissonnette on May 26, 2008, 1:11 am
Please log in for more thread options
>
> on the MB> keyboard. This was the result:
>
>
> >> /^\+?-?\d?\.?\d*$/
>
> MB> Apologies for following up my own post: I just realized the
> above has a MB> flaw: It matches on or zero beginning digits (.4 or
> 0.4) but not two digits MB> or more (22.4)
>
> MB> This works better:
>
> MB> /^\+?-?(\d?|\d+)\.?\d*$/
>
> the middle part is silly. it matches 0 or 1 digit OR one or more
> digits. that is the same as 0 or more digits which is \d* all by
> itself.
>
> but you can't use \d*\.?\d* as that will match the empty string (as
> will your regex above). everything in yours is optional. look at this:
>
> perl -ne 'print "yes\n" if /^\+?-?(\d?|\d+)\.?\d*$/'
>
> yes
> +
> yes
> +-
> yes
>
> note that the blank line was input. as i said, matching decimal
> numbers is not trivial. use regexp::common as it has solved that
> problem.
Many thanks for the pointers and solution - I will indeed look into
regexp::common
At the very least, it's been instructional/educational - As I mentioned,
I've got a few bad habits and long-ways-around-the-bush to correct :)
--
Marc Bissonnette
Looking for a new ISP? http://www.canadianisp.com Largest ISP comparison site across Canada.
|