Gunnar Hjalmarsson said:
Chris said:
Gunnar said:
Tintin wrote:
Would be useful if it matched valid IP addresses.
1.1.1.1.1 is not a valid IP address.
It doesn't match that either.
my $re =
qr/\b((\d|1{0,1}\d{2}|2[01234]\d|25[012345])\.){3}(\d|1{0,1}\d{2}|[01234]\d|25[012345])\b/;
my $ip = '1.1.1.1.1';
print "$1\n" if $ip =~ /($re)/;
Outputs:
1.1.1.1
You want to try to edit more or less randomly things that aren't valid
IPs so that they'll be valid IPs?
Didn't say that. All I said was that it doesn't match '1.1.1.1.1'.
It should match the first valid IP address it meets. So far it's up
to specification as a regex. I haven't looked closely at it, it's much
too uninviting. A regex isn't a good tool to check numeric ranges,
and it shows. You don't see what checks what, so it's hard to tell
if it's correct.
If this must be done, write a simple regex that matches anything that
vaguely looks like an IP addr. /[.\d]+/ might do, but you could get more
sophisticated, still keeping the regex simple. Then have a water-proof
checker that decides if a string is really valid. Write that based on
split and numeric comparisons, so it's easy to see what checks are done
where. Combine the two as needed to efficiently extract any number of IP
addresses from any amount of text.
Or use an industrial strength regex from Regex::Common.
Anno