Number::Range::Regex - create regular expressions that check for integers in a given range
TODO: this is out of date
use Number::Range::Regex; my $range = regex_range( 15, 3210 ); if( $jibberish =~ /$range/ ) { print "your jibberish contains an integer between 15 and 3210"; } if( $num =~ /^$range$/ ) { print "$num is an integer between 15 and 3210"; } if( $line =~ /^\S+\s+$range\s/ ) { print "the second field is an integer between 15 and 3210"; } my $octet = regex_range(0, 255); my $ip4_match = qr/^$octet\.$octet\.$octet\.$octet$/; my $range_96_to_127 = regex_range(96, 127); my $my_slash26_match = qr/^192\.168\.42\.$range_96_to_127$/; my $my_slash19_match = qr/^192\.168\.$range_96_to_127\.$octet$/;
which is more legible - this?
$date =~ m/^0*(?:[1-9]|[12][0-9]|3[01])\/0*(?:[0-9]|1[012])$/;
or this?
my $day_range = regex_range(1, 31); my $month_range = regex_range(1, 12); $date =~ m/^$day_range\/$month_range$/;
(bonus points if you spotted the bug)
It's usually better to check for number-ness only in the regular expression and verify the range of the number separately, eg: $line =~ /^\S+\s+(\d+)/ && $1 > 15 && $1 < 32; but it's not always practical to refactor in that way.
If you like one-liners, something like the following may suit you... m{^${\( regex_range(1, 31) )}\/${\( regex_range(1, 12) )}$} but, for readability's sake, please don't do that!
Non-negative integers only for now.
Please report any bugs or feature requests through the web interface at http://rt.cpan.org.
Brian Szymanski <ski-cpan@allafrica.com> -- be sure to put Number::Range::Regex in the subject line if you want me to read your message.
perl(1), Number::Range, etc.
To install Number::Range::Regex, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Number::Range::Regex
CPAN shell
perl -MCPAN -e shell install Number::Range::Regex
For more information on module installation, please visit the detailed CPAN module installation guide.