lacas lacas - 4 months ago 13
Java Question

Java replaceAll with accents

So basically I have a string like this:

"orom asd örom asddfkdjk öröm lkkllkl Örom oRÖM"


I want to search the word "orom" in it, and I need to get back this string:

"<font color='#ff0000'>orom</font> asd <font color='#ff0000'>örom</font> asddfkdjk <font color='#ff0000'>öröm</font> lkkllkl <font color='#ff0000'>Örom</font> <font color='#ff0000'>oRÖM</font>"


How can I do this with a simple replaceAll and a regex?

Answer
String str = "orom asd örom asddfkdjk öröm lkkllkl Örom oRÖM";
System.out.println(str.replaceAll("([oOöÖ][rR][oOöÖ][mM])", "<font color='#ff0000'>$1</font>"));    

That should work for you and give you the following.

<font color='#ff0000'>orom</font> asd <font color='#ff0000'>örom</font> asddfkdjk <font color='#ff0000'>öröm</font> lkkllkl <font color='#ff0000'>Örom</font> <font color='#ff0000'>oRÖM</font>