Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Re: Re: Checking whether a $var is a number

by Ted Nitz (Chaplain)
on Sep 05, 2001 at 01:01 UTC ( #110160=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Checking whether a $var is a number
in thread Checking whether a $var is a number

that's easy to fix (untested):
qr/(?:+|-)?\d+(?:\.)?(?:\d+)?(?:[Ee](?:+|-)?\d+)/

piece by piece:

(?:+|-)? # Optional leading + or - \d+ # One or more digit (?:\.)? # A trailing . 12. is a number I think (?:\d+)? # The part after the decimal (?:[Ee](?:+|-)?\d+) # E or e followed by an optional + or - and one or + more digit
I don't think I left anything out. Now, this won't match .4 as a number.... You can build a regex that will handle that case properly as well. But the simple fix of making the part before the decimal optional causes another bug, it maks every part optional, so the null string would be a match, and since every string starts with a null string, every string would match. Since all we have is a number, we could anchor to the begenning and end of the value, but then, the empty string would still match. Thinking back to his origonial question, this should be anchored to the front and back, so more like qr/^...$/.
-Ted


Comment on Re: Re: Re: Checking whether a $var is a number
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://110160]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-09-17 20:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (99 votes), past polls