when replacing non-leaf parts of the pattern, keep the subpattern if it has the correct code