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 } }