If($str =~ m/(pattern2)/gi) { $var1 = &function($1); If($str =~ m/(pattern1)pattern2/gi){ $str = &function($1); } }