Generally speaking, it's better to match any number, then make sure it's in range.
# Supports ranges in any number of bytes. sub parse_ip_mask { my ($ip_mask) = @_; my $checks = ''; my $check_num = 0; my $ip_re = join('\\.', map { if (/\[(\d+)-(\d+)\]/) { $check_num++; $checks .= " && \$$check_num >= $1"; $checks .= " && \$$check_num <= $2"; '(\\d+)' } else { $_ } } split(/\./, $ip_mask)); return eval "sub { (\$_[0] || \$_) =~ /^$ip_re\$/$checks }"; } my $ip_check = parse_ip_mask('131.202.1.[3-4]'); foreach (qw( 131.202.1.2 131.202.1.3 131.202.1.4 132.202.1.4 )) { print(&$ip_check() # Uses $_ if no arguments are specified. ? "$_ matches.$/" : "$_ doesn't match.$/" ); } __END__ output ====== 131.202.1.2 doesn't match. 131.202.1.3 matches. 131.202.1.4 matches. 132.202.1.4 doesn't match.
It's possible to do this without eval. Give me a few minutes.
In reply to Re: converting user friendly regex to perl friendly
by ikegami
in thread converting user friendly regex to perl friendly
by Random_Walk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |