Q:

Java replaceAll regex read from XML

So I have read 2 strings from an xml file. The xml string is:
<regex code="bla[. ]?(.*)\n" replacement="cpe_ip_learning_table:\\n$1\\n "/>

This reads into two separate variables for a replaceAll call. i.e:
str = str.replaceAll(code, replacement);

However, even when I try a \n as a replacement instead of a \\n it still does not insert a newline.
I am using a DOM DocumentBuilder to read the XML file into two separate strings. I have tried using Matcher.quoteReplacement as well to no avail. Has anyone run into this issue and how have they solved it?

A:

I've experienced this same behavior also, and the only way I found to fix it was by doing a replaceAll on the replacement string itself:
rep = replacement.replaceAll("\\\\n", "\n")
str = str.replaceAll(code, rep);

Strings returned by DOMDocument ignore newline (\n) constructs for some strange reason.

