use List::Util qw( first ); my @pattern_prefix = ( { pat => qr{{[\w/-]+MMS-Apps/eMedSample}i, pre => 'MMS-Apps/eMedSample/WwwRoot', }, { pat => qr{...}i, pre => '...' }, ); my $match = first { $path =~ $_->{pat} } @pattern_prefix; $ImageFilePath = $match ? $match->{pre} . $imagefile : "Not sure what the path is here: $path\n";