First tested: ------------- $str = 'abac 123 afa123f'; $str =~ s|\B123\B|***|g; print $str; output i got as i expected: --------------------------- abac 123 afa***f Second tested: -------------- $str = 'abac ~~~ afa~~~f'; $str =~ s|\B\&\#x0007E\;\&\#x0007E\;\&\#x0007E\;\B|~~~|g; print $str; output i got: ------------- abac ~~~ afa~~~f expected output: ---------------- abac ~~~ afa~~~f