Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: only want numerics in field

by davido (Archbishop)
on Apr 21, 2014 at 16:37 UTC ( #1083041=note: print w/ replies, xml ) Need Help??


in reply to only want numerics in field

You could use Regexp::Common's "number" extension to detect reals, which is what you seem to be seeking:

if( $string =~ /^$RE{num}{real}$/ ) { print "Match!\n"; } else { print "Reject!\n"; }

You don't want to invent this yourself. Just look at the regular expression that $RE{num}{real} contains:

perl -MRegexp::Common=number -E 'say $RE{num}{real}' (?:(?i)(?:[-+]?)(?:(?=[.]?[0123456789])(?:[0123456789]*)(?:(?:[.])(?:[ +0123456789]{0,}))?)(?:(?:[E])(?:(?:[-+]?)(?:[0123456789]+))|))

...better off using a solution from Regexp::Common, or the looks_like_number() utility Limbic~Region suggested, or both.


Dave


Comment on Re: only want numerics in field
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2015-07-01 21:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (22 votes), past polls