Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Matching string containing values between 0 and 100 inclusive

by Anonymous Monk
on Mar 17, 2009 at 09:41 UTC ( [id://751125]=perlquestion: print w/replies, xml ) Need Help??

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

What regular expression can I use to match a string containing the following values: "0" ... "9" ... "10" .. "99" .. "100" and all in between. Thanks!
  • Comment on Matching string containing values between 0 and 100 inclusive

Replies are listed 'Best First'.
Re: Matching string containing values between 0 and 100 inclusive
by Corion (Patriarch) on Mar 17, 2009 at 09:45 UTC

    What have you tried? This sure looks like homework, so have a look through perlre.

Re: Matching string containing values between 0 and 100 inclusive
by andreas1234567 (Vicar) on Mar 17, 2009 at 10:27 UTC
    Would your professor reject if ($x >= 0 && $x <= 100) { .. }?
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
Re: Matching string containing values between 0 and 100 inclusive
by moritz (Cardinal) on Mar 17, 2009 at 09:48 UTC
    100|[1-9][0-9]|[0-9]. If you want to allow 00 as well, it's easier: 100|[0-9]{1,2}.

    Update: of course this needs to be anchored somehow, otherwise it will match substrings of larger numbers.

Re: Matching string containing values between 0 and 100 inclusive
by poolpi (Hermit) on Mar 17, 2009 at 10:57 UTC

    Are you dealing with set of integers?

    #!/usr/bin/perl -w use strict; use Set::IntSpan; my ($run_list, $set, $n); $run_list = '0-100'; $n = 13; eval { $set = new Set::IntSpan $run_list }; $@ and print "$@: bad run list\n"; member $set $n and print "$n is a member of my set\n";


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
Re: Matching string containing values between 0 and 100 inclusive
by Your Mother (Archbishop) on Mar 17, 2009 at 16:36 UTC

    You already got some good Perl answers above, I'm just being a language nag: "values between 0 and 100 inclusive."

    I know that construction is gaining popularity. Don't let it. If you're between your mom and your dad, can you also be inside them? Between has a meaning and it excludes "inclusive." "From 0 to 100" is fine and strongly implies "inclusive" and if it's not strong enough, you can tack some extras on that one. Leave "between" its dignity and exact meaning, inclusive.

Re: Matching string containing values between 0 and 100 inclusive
by ikegami (Patriarch) on Mar 17, 2009 at 23:46 UTC

    "7.8" is in between. Did you mean to include it?
    Is "02" allowed?

      Good point. Based on the problem description as given in the OP, it looks like there should be a range of values accepted between 9 and 10, as well as between 99 and 100.

      (Oops... if the instructor who handed out the homework was expecting that little detail to turn it into a "trick question", I probably just gave away the surprise.)

      Helpful hint for the truly ambitious student: get extra credit by making sure the regex covers e and π as well -- both of them are in the specified range. ;)

Re: Matching string containing values between 0 and 100 inclusive
by grinder (Bishop) on Mar 18, 2009 at 08:41 UTC

    perl -MRegexp::Assemble -le 'print Regexp::Assemble->new->add(0..100)->as_string'

    ...produces...

    (?:1(?:[123456789]|0?0)?|2\d?|3\d?|4\d?|5\d?|6\d?|7\d?|8\d?|9\d?|0)

    This could be simplified by gathering the 2\d?, 3\d?... subpatterns and factoring them into [2-9]\d?, but that would be less efficient since character classes are slower, and in 5.10 this would bypass demerphq's trie optimisation.

    On the plus side, while it will examine all the 10 primary alternatives during a fail, it's failing on EXACT matches, which is fast.

    This lesson was brought to you by Stupid Regexp Trick I Can Play.

    • another intruder with the mooring in the heart of the Perl

      You should use Regexp::List from the same for distro when you have strings rather than patterns. It doesn't affect the outcome in this case, but let's not get into the habit of using the wrong tool.

      perl -MRegexp::List -le'print Regexp::List->new->list2re(0..100)'

      in 5.10 this would bypass demerphq's trie optimisation.

      In 5.10, the equivalent of Regexp::Assemble would be:

      my $re = join '|', 0..100;

      and the equivalent of Regexp::List would be:

      my $re = join '|', map quotemeta, 0..100;
Re: Matching string containing values between 0 and 100 inclusive
by codeacrobat (Chaplain) on Mar 17, 2009 at 23:36 UTC
    For educational purpose ( see ""(??{ code })" section of perlre). In real code, please use KISS_principle if ($x >= 0 && $x <= 100).
    perl -le ' for $num (qw(-1 0 7 12 88 100 123)) { if ($num=~ /^(\d+)(??{$^N >=0 && $^N <= 100 ? "" : "(?!)"})$/) { print $1; } } '
    update: added if clause. update2: added perlre reference.

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});

      That compiles a regexp pattern every run.

      /^(\d+)(??{$^N >=0 && $^N <= 100 ? "" : "(?!)"})$/

      should be

      /^(\d+)(?(?{ $^N < 0 || $^N > 100 })(?!))$/

      And match failures would be cheaper still as

      /^(\d+)$(?(?{ $^N < 0 || $^N > 100 })(?!))/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found