|
Posted by Marc Bissonnette on May 25, 2008, 11:31 pm
Please log in for more thread options
>
> MB> So, here's my understanding of
>
> MB> /^-?\d+\.?\d*$/;
>
> MB> / start the search pattern
> MB> ^ match the beginning of the line
>
> beginning of string in this case.
>
> MB> -? match a minus sign once or not at all
> MB> \d+ match a digit character zero or more times
>
> one or more times. you must have digits before the decimal point.
> MB> \.? match a decimal once or not at all
> MB> \d match a digit character
> MB> *$ all to the end of the line
>
> that is \d* which is zero or more digits. then comes $ which is end of
> the string (or before an ending newline.
>
> MB> / end the search pattern
> MB> I *think* this means
> MB> "Warn unless the input *only* matches minus signs, digit
> MB> characters and decimals, from the beginning to the end of the
> string"
>
> well it has its flaws. it matches most numbers but what about just
> fractional numbers like .9? it fails there since it requires digits
> before any decimal point. also it doesn't allow a leading + sign.
I noticed that when I was goofing around with it locally;
In readin your points and thinking about it (now that I understand it a
bit better), this seems to work (leading pluses or minuses, as well as
leading decimals, such as .5
/^\+?-?\d?\.?\d*$/
> look at Regexp::Common on cpan. i am sure it has a number validation
> regex in there. it is trickier than your example here as it allows all
> number formats (including exponents).
I'll definitely take a peek in there when the need arises - For now, I'm
helping a friend out by trying to automate some of their purchase orders,
packing slips and job accounting summaries, so I think the numerical data
will suffice being limited to positives and negatives :)
>
> MB> Now that section of my code is:
>
> MB> if ($in !~ /^-?\d+\.?\d*$/) {
> MB> push @missing,'Hours contains non-numerical data';
> MB> $missing = 1;
> MB> }
>
> the $missing = 1 is a red flag as boolean flags are poor coding IMO.
> you have @missing which supposedly contains error strings so just
> check that if it isn't empty.
Ya know, that thought honestly popped in my mind as soon as I hit send :)
It's an ingrained bad habit I'll have to work myself out of.
> MB> Comments and general pointing and laughing welcome. :)
>
> MUAHAHAHAHAHAHHAH!!!
:)
Better a MUAHAHAHAHHAHA than a RTFM :)
--
Marc Bissonnette
Looking for a new ISP? http://www.canadianisp.com Largest ISP comparison site across Canada.
|