in reply to Regex for IP and Optional Port

Save yourself some regex complexity: use \d rather than [0-9], and you can also be more specific about the number of digits you expect. You should first extract the IP addresses (optionally with port numbers), and then try to separate them in a subsequent step. For example,

if ( $inLine =~ / ( # $1 - first IP \d{1,3} # 1-3 digits (?: # grouping for rep count \. # dot \d{1,3} # 1-3 digits ){3} # three more times (?: # optional extra grouping \/ # a slash \d+ # 1st port number )? # close extra grouping ) # close group around 1st IP \s+ # whitespace \w+ # a word \s+ # more whitespace ( # $2 - 2nd IP \d{1,3} # 1-3 digits (?: # grouping for rep count \. # dot \d{1,3} # 1-3 digits ){3} # three more times (?: # optional extra grouping \/ # a slash \d+ # 2nd port number )? # close extra grouping ) # close group around 2nd IP /x ) # specify /x { # matched - now check for port numbers my @ips = ( $1, $2 ); my @ports; foreach ( @ips ) { my ( $ip, $port ) split m{/}; # try to split IP on '/' push @ports, $port; # may be undef $_ = $ip if $port; # replace IP if got port } }

dmm

If you GIVE a man a fish you feed him for a day
But,
TEACH him to fish and you feed him for a lifetime