[Israel.pm] RegEx question

Gabor Szabo szabgab at gmail.com
Tue Jan 8 06:33:48 PST 2008


if you just want to make sure that it looks like your example then here it is:
my $one = qr/\d+(-\d+)?/;

/^$one(,$one)*$/


if you also would like to make sure that ranges are correct
(small-big) and that the list is not overlapping and the ranges
are actually growing then IMHO you are better off splitting the whole
thing and writing something longer:

sub check_ranges {
my $str = shift;
my $last;
my @ranges = /,/, $str;
foreach my $r (@ranges) {
    if ($r =~ /^\d+$/) {
       if (not defined $last) {
          $last = $r;
       } else {
           if ($last >= $r) {
              die "Invalid order";
           }
       }
    } elsif ($r =~ /^(\d+)-(\d+)$) {
       my ($low, $high) = ($1, $2);
       die if $high < $low;
       if (not defined $last) {
          #... some more code here
      }
    } else {
         die "Invalid part $r";
    }
}
}


now someone please point us to the CPAN module that does this :-)

Gabor

On Jan 8, 2008 4:18 PM, Yossi Itzkovich <Yossi.Itzkovich at ecitele.com> wrote:
> Hi,
>
> I need to build a regex to validate a list of number or ranges, such as
> in giving when sending to printing, for exmaple:
>
> 1,4,6-9,13-15,19
>
> Is there a way to do it in one regex ?
>
> Thanks
>
> Yossi


-- 
Gabor Szabo
http://www.szabgab.com/
Perl Training in Israel  http://www.pti.co.il/
Profile: http://www.linkedin.com/in/szabgab



More information about the Perl mailing list