in reply to Re: A function to determine if a string is numeric
in thread A function to determine if a string is numeric

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

Replies are listed 'Best First'.
Re^3: A function to determine if a string is numeric
by Tux (Canon) on May 18, 2020 at 12:17 UTC

    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
Re^3: A function to determine if a string is numeric
by hippo (Archbishop) on May 18, 2020 at 12:19 UTC

    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";