my $str1 = "(View like all data) AND (Path not like /usr/bin)"; # working my $str2 = "(View like all data AND Path not like /usr/bin)"; # not working while ($str1 =~ /\(\s*[\W\w]+?(like|not like)\s+([^%][\w\W][^%]+?)\s*\)/) { my $new = "%".$2."%"; $str =~ s/$2/$new/; }