our $matched_key; #global to hold the key that matched, off the top of my head I don't think a 'my' variable would work. for (keys($$args)) { $string .= "$_(?{$matched_key = $_})|"; }