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

Simple regex should match number

by Anonymous Monk
on Feb 01, 2012 at 07:10 UTC ( [id://951132]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I expect the string "1" to match the following regex but it doesn't.

my $str = "1"; if ($str =~ m/-?\p{Nd}/){ print "Matches\n"; else { print "Doesn't match\n"; }

I am using Perl v5.10.0

Replies are listed 'Best First'.
Re: Simple regex should match number
by repellent (Priest) on Feb 01, 2012 at 07:41 UTC
    Really weird, looks like a bug:
    use Test::More; ok( 1 =~ m/\p{Nd}/ ); ok( 1 =~ m/^-?\p{Nd}/ ); ok( 1 =~ m/-?\p{Nd}+/ ); ok( 1 =~ m/-?\p{Nd}/ ); ok( 1 =~ m/-*\p{Nd}/ ); ok( 1 =~ m/x?\p{Nd}/ ); ok( 1 =~ m/x?y?\p{Nd}/ ); done_testing(); __END__ ok 1 ok 2 ok 3 not ok 4 # Failed test at ./t.pl line 16. not ok 5 # Failed test at ./t.pl line 17. not ok 6 # Failed test at ./t.pl line 18. not ok 7 # Failed test at ./t.pl line 19. 1..7 # Looks like you failed 4 tests of 7.

    $ perl -v This is perl, v5.10.0 built for darwin-thread-multi-2level
      That's a known bug, and, AFAIK, fixed in 5.14.0. It's certainly fixed in 5.14.2.
Re: Simple regex should match number
by davido (Cardinal) on Feb 01, 2012 at 07:25 UTC

    According to what I've read in perlretut (or at least how I interpret it), \p{Nd} says match a non-digit. Maybe you want \P{Nd} which is "Match anything that is NOT a non-digit (double negative equals a positive). But that's a rather convoluted way of saying \p{Digit} or \p{IsDigit}.I haven't tested, so I'll leave that to you to decide which works best.

    Update: I guess I should have tested. :) It does match for me. ...and now I'll have to re-read the paragraph in perlretut. Perl 5.14.2. (And I see now that I did misread: \p{Nd} is indeed an affirmative, and \P{Nd} matches non-digit. So I do get a match, and you don't... we're back to square one. Move on to the next response. ;)


    Dave

Re: Simple regex should match number
by Anonymous Monk on Feb 01, 2012 at 07:58 UTC
    Yup, it works in 5.14.1 but doesn't work in 5.12.2
    my $str = "1"; if ($str =~ m/-?\p{Nd}/){ print "Matches\n"; } else { print "Doesn't match\n"; } __END__ Doesn't match

    What does work in 5.12.2 (and I assume 5.10) is adding a quantifier, say m/-?\p{Nd}{1}/

Re: Simple regex should match number
by JavaFan (Canon) on Feb 01, 2012 at 11:52 UTC
    Known bug. Upgrade to 5.14.

      Thanks. How do you find the bugs per release. I'm looking at http://rt.perl.org/rt3/Public and its one longggg list.

        Other than rt.perl.org, I don't think anyone keeps official records. Note that noone knows all the bugs -- not every bug gets discovered or reported.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://951132]
Approved by GrandFather
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-25 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found