Hi!

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.

|use Regexp::Common qw/net/; ||my $ip_port = "|1.1.1.1:80 <http://1.1.1.1:80>"; say ($|ip_port| =~ |/^||$RE||{net}{IPv4}|$/); # false

|my $ip = ||strip_port($||ip_port);|
||

say ($|ip|  =~|/^||$RE||{net}{IPv4}|$/);  # true

|sub strip_port {|

|    my ($ip_addr) = @_;|

|    state $port_re ||= ||qr/^||[.:][1-9][0-9]{1,4}$/;|

|||$ip_addr ||=~ s/$port_re//;|

|    return $ip_addr;
|

|}
|


14.07.16 21:38, Chris Knipe пишет:
Hi,

Any modules available that can:
- Parse IPv4/IPv6 addresses that includes ports (1.1.1.1:80 <http://1.1.1.1:80> & ::1.80 for example)
  - Convert the IPv4 and IPv6 to integers

There's plenty around to convert to integers, validate the address, etc. Not a lot (that I could find) that can handle an address that includes the port portion. Net::IP for example sees the above mentioned two examples as invalid addresses (and rightfully so I suppose).

Is there perhaps an intelligent way to split() the address from the port that would work for both IPv4 as well as IPv6? Should be possible with a regex I guess, but them regex'es ain't one of my strong points.

--

Regards,
Chris Knipe

Reply via email to