Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to Replace & with &

Tags:

regex

perl

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.

like image 776
user380979 Avatar asked Nov 29 '22 04:11

user380979


1 Answers

You can use a negative look-ahead assertion:

$str =~ s/&(?!\w+;)/&/g;
like image 55
Eugene Yarmash Avatar answered Dec 06 '22 07:12

Eugene Yarmash