use strict; use warnings; my $string = 'A000 A123 A567 B789 B888 B999 B000 A998 A999'; my $regex = qr/((A|B)(\d{3}))/; while ( $string =~ /$regex/g ) { if ( $2 eq 'A' ) { if ( $3 == 000 or ($3 >= 123 and $3 <= 456) or $3 == 999 ) { print "$1 valid string\n"; } else { print "$1 rejected invalid range\n" } } elsif ( $2 eq 'B' ) { if ( $3 == 000 or ($3 >= 789 and $3 <= 888) ) { print "$1 valid string\n"; } else { print "$1 rejected invalid range\n" } } else { print "$3 seems valid to me\n" } }
update:
something else along the same lines:
or if you are going only for regex only solutions according to this the following regex should be valid in both:while ( $string =~ /((?:A|B)\d{3})/g ) { if ( $1 eq 'A000' or ( $1 ge 'A123' and $1 le 'A456' ) or ( $1 ge 'A999' and $1 le 'B000' ) or ( $1 ge 'B789' and $1 le 'B888' ) ) { print "$1 matched\n"; } else { print "$1 did not match\n" }
/(A(?:000|(?:1(?:2[0-3]|[01]\d))|[23]\d{2}|4(?:[0-4]\d|5[0-6])|999)|B( +?:000|789|79\d|(?:8[0-7]\d|88[0-8]+)))/
-enlil
In reply to Re: Need Set-like Regex for Perl and Java (golf OK)
by Enlil
in thread Need Set-like Regex for Perl and Java (golf OK)
by QM
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |