$hop =~ s/(r(eg)e(xp)/mutilate($1,$2)/eg #### while ($hop =~ m/r(eg)e(xp)/g) { // handle stuff // to replace, substr($hop,length $`,length $&) = $replace; and think about slowness of $`, $& and all // to rewind, pos($hop)=$location; }