in reply to converting user friendly regex to perl friendly

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.

Replies are listed 'Best First'.
Re^2: converting user friendly regex to perl friendly
by ikegami (Patriarch) on Oct 15, 2004 at 18:25 UTC

    A version without eval:

    # Supports ranges in any number of bytes. sub parse_ip_mask { my ($ip_mask) = @_; my @checks; foreach (split(/\./, $ip_mask)) { if (/\[(\d+)-(\d+)\]/) { push(@checks, [ 0+$1, 0+$2 ]); } else { push(@checks, [ 0+$_, 0+$_ ]); } } return \@checks; } sub ip_check { my ($ip, $checks) = @_; my @ip = split(/\./, $ip); my $i; for ($i=0; $i<4; $i++) { return undef if ($ip[$i] < $checks->[$i][0]); return undef if ($ip[$i] > $checks->[$i][1]); } return 1; }

    Example usage #1, check if an IP is in range:

    my $ip_mask = 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($_, $ip_mask) ? "$_ 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.

    Example usage #2, finding matching IPs in a line:

    my $ip_mask = parse_ip_mask('131.202.1.[3-4]'); foreach ( 'Connection established to 131.202.1.2', 'Connection established to 131.202.1.3', 'Connection established to 131.202.1.4', 'Connection established to 132.202.1.4', ) { my ($ip) = /(\d+\.\d+\.\d+\.\d+)/; print("Found matching ip $ip.$/") if (ip_check($ip, $ip_mask)); } __END__ output ====== Found matching ip 131.202.1.3. Found matching ip 131.202.1.4.