my %patterns = ( 123 => [ 1, 2, 3 ], 312 => [ 3, 1, 2 ] ); @matches = $string =~ /regexp/; $string = join "", @matches[ @{ $patterns{$patternkey} } ];