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.
| [reply] |
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]
| [reply] [d/l] |
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. | [reply] [d/l] [select] |
Re: Matching string containing values between 0 and 100 inclusive
by poolpi (Hermit) on Mar 17, 2009 at 10:57 UTC
|
#!/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
| [reply] [d/l] |
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.
| [reply] |
Re: Matching string containing values between 0 and 100 inclusive
by ikegami (Patriarch) on Mar 17, 2009 at 23:46 UTC
|
| [reply] [d/l] [select] |
|
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. ;)
| [reply] |
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
| [reply] [d/l] [select] |
|
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;
| [reply] [d/l] [select] |
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']});
| [reply] [d/l] [select] |
|
/^(\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 })(?!))/
| [reply] [d/l] [select] |