my $text='abc-123'; if ( $text =~ /(abc)-(\d{3})/) { say "\$1 is $1"; # rather than printing the matches, as this does, say "\$2 is $2"; # you could push the matches from each iteration # over the full text to two arrays } # i.e., something like... push @IPs, $1; push @MACs, $2; }