Hi.
I used Regexp::Common here just to illustrate that before strip_port
$ip_addr string doesn't match standard IPv4 regular expression and after
it matches.
You can construct single regular expression to test the whole string at
once - something like
use Regexp::Common qw/net/;
|$port_re = "[.:]|||[1-9][0-9]{1,4}|";
|
my $re = qr|/^(||$RE||{net}{IPv4}|||||$RE||{net}{IPv6})(?:$port_re)?|$/;
if ($string =~ $re) {
say "$string is valid ip";
}
14.07.16 23:46, Chris Knipe пишет:
Hi,
On Thu, Jul 14, 2016 at 10:28 PM, Илья Рассадин <elcaml...@gmail.com
<mailto:elcaml...@gmail.com>> wrote:
It's really simple regexp that can handle this, I wrote simple
example to illustrate the idea.
Of course, this $port_re can match invalid port (all port numbers
which higher than 65536). If it's critical to your case, you can
adjust strip port function.
Yes, that will work for IPv4, not for IPv6 (Regexp::Common:Net needs a
different pattern for a IPv6 regexp). It's not as simple as that.
You first need to reliably determine whether it's an IPv4 or IPv6
address (both contain . and : which is generally used for this test),
and whether it's IPv4 or IPv6, that will determine how to strip off
the port (such as using Regexp::Common for example)...
Once you know for certain that you're dealing with an IPv4 OR an IPv6
address, yes, it's easy to strip the port off. I guess the question
wasn't clear then. How would you differentiate between a IPv4:port
and IPv6.port, as both contain the same set of numbers. Assuming IPv4
is decimal only also won't work as 1:2:3:4::1 is also a perfectly
legitimate IPv6 address.
Would need to be some kind of regexp checking the amount of "." vs.
the amount of ":" characters I guess. IPv4 = 3 dots + 1 colon, whilst
IPv6 = at least two colons and one dot. Once you know whether you're
dealing with IPv4 or IPv4, a simple split() would be enough to cut the
port... The question is more towards determining reliably what you're
working with.
--
Chris.