user380979 user380979 - 6 months ago 36
Perl Question

Regular Expression to Replace & with &

I have a string which have & like the one below.

"This R&M & Exapmle  . It is very big & Complicated &146; example."


I wants to replace
&
with
&
but when I use
$str =~ s/&/&/ig;
which give the following output.

"This R&M & Company  . It is very big & CMM Level3 &146; Organization."


And I am expecting this one.

"This R&M & Company  . It is very big & CMM Level3 &146; Organization."


Please help me I don't have any idea how to fix it.

Answer

You can use a negative look-ahead assertion:

$str =~ s/&(?!\w+;)/&/g;