user380979 user380979 - 2 years ago 123
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 Source

You can use a negative look-ahead assertion:

$str =~ s/&(?!\w+;)/&/g;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download