Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

matching between 1 to 8 digits but not more

by ozgurp (Beadle)
on Aug 20, 2009 at 08:50 UTC ( #790029=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, I have been trying to match a line that has from 1 to eight digits and if the line has more than eight digits then I want to report an error. I have tried the following but it still matches more than 8 digits. and If I try {,8} than only matches 8 digits not less, not more! Could you please help?
if($Number =~ m/\d{1,8}/){ $N = $Number; }else{ print("line has more than 8 digits!\n" }

Replies are listed 'Best First'.
Re: matching between 1 to 8 digits but not more
by moritz (Cardinal) on Aug 20, 2009 at 09:11 UTC
    Note that a string with 9 digits also contains 8 digits. so you'll have to be more careful.

    A good way to re-formulate the question is: does the string contain 1 to 8 digits that are neither followed nor preceded by digits?

    When you ask it like that, it's easy to formulate as a regex:

    m/(?<! \d ) \d{1,8} (?! \d )/x

    For an explanation of the look-ahead and look-behind groups see perlre.

    Perl 6 projects - links to (nearly) everything that is Perl 6.
Re: matching between 1 to 8 digits but not more
by FunkyMonk (Chancellor) on Aug 20, 2009 at 08:57 UTC
    if (m/\d{9,}/) { # more than 8 digits } elseif (m/\d/) { # 8 or less digits else { # no digits }

Re: matching between 1 to 8 digits but not more
by cdarke (Prior) on Aug 20, 2009 at 09:08 UTC
    You need to consider what comes before and after the digits (\D is a non-digit):
    use strict; use warnings; while( my $Number = <DATA> ) { chomp $Number; if($Number =~ /(\D|^)\d{1,8}(\D|$)/){ print "$Number is OK\n" } else { print "$Number has more than 8 digits!\n" } } __DATA__ 1234567 12345678 123456789 grunge 1234567 grunge grunge 12345678 grunge grunge 123456789 grunge
    Gives:
    1234567 is OK 12345678 is OK 123456789 has more than 8 digits! grunge 1234567 grunge is OK grunge 12345678 grunge is OK grunge 123456789 grunge has more than 8 digits!
Re: matching between 1 to 8 digits but not more
by Old_Gray_Bear (Bishop) on Aug 20, 2009 at 09:13 UTC
    I think you want to add anchors to your regex --
    if($Number =~ m/^\d{1,8}$/){ $N = $Number; } else { print("line has more than 8 digits!\n"); }
    This seems to satisfy the requirement -- '12345678' and '123456' don't print; '12345654321' does.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: matching between 1 to 8 digits but not more
by james2vegas (Chaplain) on Aug 20, 2009 at 09:01 UTC
    if ($Number =~ m/\d\d{8,}/) { print ("line has more than eight digits!\n"); }

    This only really does what your message indicates, i.e. it doesn't test for zero digits.
Re: matching between 1 to 8 digits but not more
by JavaFan (Canon) on Aug 20, 2009 at 10:04 UTC
    Assuming you mean you don't want more than 8 digits in a row:
    if ($Number =~ /\d{9}/) { print "Line has more than 8 digits!\n" } elsif ($Number =~ /\d/) { $N = $Number; } else { print "Line has no digits at all!\n"; }
      If you don't care about consecutiveness:

      $digits = () = 'a1b2c3d4 e5f6g7h8i9' =~ /\d/g;

Re: matching between 1 to 8 digits but not more
by bpoag (Monk) on Aug 21, 2009 at 22:41 UTC
    Your test should be simply:

    if ($Number!~/^[0-9]{1,8}$/) { ## blah blah }


    ..I'm surprised so many people are having a problem with this.

    In English: For anything that doesn't explicitly match a string of numbers, and only numbers, of at least 1 character in length, but no more than 8 characters, do ##blah blah.

    Cheers,
    Bowie

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2022-05-20 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (73 votes). Check out past polls.

    Notices?