Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: A function to determine if a string is numeric

by trwww (Priest)
on Feb 25, 2012 at 06:51 UTC ( #956069=note: print w/replies, xml ) Need Help??


in reply to A function to determine if a string is numeric

There is also Regexp::Common. Ex:

if ( $RE{num}{int}->matches($text) ) {...}
if ( $RE{num}{real}->matches($text) ) {...}

This library is cool because you can refine/define exactly what you mean by number.

Replies are listed 'Best First'.
Re^2: A function to determine if a string is numeric
by zborsos (Initiate) on May 18, 2020 at 11:28 UTC

    use Regexp::Common; use Data::Dumper; use v5.30.0; my $value = "+a1.2cd"; if ( $RE{num}{int} -> matches($value) ) { say "INTEGER patern:\t".$RE{num}{int}; say $value; say "It is Integer"; }elsif ( $RE{num}{real}->matches($value) ) { say "REAL patern:\t".$RE{num}{real}; say $value; say "It is Real"; }else{ say "It is String"; }

    I tried Regexp::Common , but seams to behave weird to me. The code above is saying :

    It is Integer

      That is documented behavior:

      NOT A BUG The various patterns are not anchored. That is, a pattern like "$RE +{num}{int}" will match against "abc4def", because a substring of the subject ma +tches. This is by design, and not a bug. If you want the pattern to be anc +hored, use something like: my $integer = $RE{num}{int}; $subj =~ /^$integer$/ and print "Matches!\n";

      Enjoy, Have FUN! H.Merijn

      The documentation explains this.

      The various patterns are not anchored. That is, a pattern like $RE {num} {int} will match against "abc4def", because a substring of the subject matches. This is by design, and not a bug. If you want the pattern to be anchored, use something like:
      my $integer = $RE {num} {int}; $subj =~ /^$integer$/ and print "Matches!\n";

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2020-07-03 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?