my ( $peer, $ip, $type, $mask, $port, $status ) = ( $line =~ / ([\w\d\-_]+) # First capture, peer name \/?\s+ # Optional trailing slash, whitespace \(? # Optional opening parenthesis ([\w\d\.]+) # Second capture, IP address \)? # Optional closing parenthesis \s+ # Whitespace \(? # Optional open parenthesis ([\w\d]+) # Third capture, type \)? # Optional ending parenthesis \s+ # Whitespace ([\w\d\.]+) # Forth capture, mask \s+ # Whitespace ([\w\d]+) # Fifth capture, port \s+ # Whitespace ([\w\d]+) # Final capture, status /x ); Matches both: rpc-pbx-per-01/ 10.46.26.252 (S) 255.255.255.255 4569 UNREACHABLE rpc-pbx-glb-01/ (Unspecified) (S) 255.255.255.255 4569 UNKNOWN