Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java/Parsing: how to replace & symbol but not html codes

Tags:

java

parsing

I need to replace all "&" symbols with "&#38" in my text file but not the html codes such as & or "

I'm currently using row = row.replace("& ", "&#38");

but, as I said also the html codes are replaced e.g. " and I don't want this.. thanks

ps. I cannot add spaces after & because I need to replace it in words such as M&M or Ella & David

like image 764
aneuryzm Avatar asked Feb 24 '11 09:02

aneuryzm


1 Answers

You could try a regex, e.g,

row = row.replaceAll("&(?![#a-zA-Z0-9]+;)", "&");

The regex replace & given that it's not followed by a sequence of '#a-zA-Z0-9' ending with ';'

like image 158
Johan Sjöberg Avatar answered Sep 24 '22 21:09

Johan Sjöberg