in reply to Filtering out IP addresses
Another approach. Identifies 'real' decimal octet IP addresses. Matches 'x' replacements to actual octet digits if that's really what you want to do.
c:\@Work\Perl\monks>perl -wMstrict -le "use Regexp::Common qw(net); ;; for my $s ( '1.2.3.4', '1.22.111.222', 'a1.2.3.4a b1.22.111.221b', '999.9.9.9', '9.9.9.999', ) { printf qq{'$s' -> }; (my $t = $s) =~ s{ (?<! \d) ($RE{net}{IPv4}) (?! \d) } { (my $ip = $1) =~ s{ \d (?= \d* [.]) }{x}xmsg; $ip; }xmsge; print qq{'$t'}; } " '1.2.3.4' -> 'x.x.x.4' '1.22.111.222' -> 'x.xx.xxx.222' 'a1.2.3.4a b1.22.111.221b' -> 'ax.x.x.4a bx.xx.xxx.221b' '999.9.9.9' -> '999.9.9.9' '9.9.9.999' -> '9.9.9.999'
Note that if you're using Perl 5.14+, the /r substitution modifier makes the expression a bit simpler.
c:\@Work\Perl\monks>perl -wMstrict -le "use 5.014; ;; use Regexp::Common qw(net); ;; for my $s ( '1.2.3.4', '1.22.111.222', 'a111.22.3.4a b1.22.111.221b', '999.9.9.9', '9.9.9.999', ) { printf qq{'$s' -> }; my $t = $s =~ s{ (?<! \d) ($RE{net}{IPv4}) (?! \d) } { $1 =~ s{ \d (?= \d* [.]) }{x}xmsgr }xmsger; print qq{'$t'}; } " '1.2.3.4' -> 'x.x.x.4' '1.22.111.222' -> 'x.xx.xxx.222' 'a111.22.3.4a b1.22.111.221b' -> 'axxx.xx.x.4a bx.xx.xxx.221b' '999.9.9.9' -> '999.9.9.9' '9.9.9.999' -> '9.9.9.999'
|
|---|