Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从XML读取Java replaceAll正则表达式_Java_Regex_Dom - Fatal编程技术网

从XML读取Java replaceAll正则表达式

从XML读取Java replaceAll正则表达式,java,regex,dom,Java,Regex,Dom,因此,我从一个xml文件中读取了两个字符串。xml字符串是: <regex code="bla[. ]?(.*)\n" replacement="cpe_ip_learning_table:\\n$1\\n "/> 但是,即使我尝试将\n替换为\\n,它仍然不会插入换行符 我使用DOM DocumentBuilder将XML文件读入两个单独的字符串。我也尝试过使用Matcher.quoteReplacement,但没有效果。有人遇到过这个问题吗?他们是如何解决的?我也经历过同样的行

因此,我从一个xml文件中读取了两个字符串。xml字符串是:

<regex code="bla[. ]?(.*)\n" replacement="cpe_ip_learning_table:\\n$1\\n "/>
但是,即使我尝试将\n替换为\\n,它仍然不会插入换行符


我使用DOM DocumentBuilder将XML文件读入两个单独的字符串。我也尝试过使用Matcher.quoteReplacement,但没有效果。有人遇到过这个问题吗?他们是如何解决的?

我也经历过同样的行为,我发现解决这个问题的唯一方法是对替换字符串本身执行
replaceAll

rep = replacement.replaceAll("\\\\n", "\n")
str = str.replaceAll(code, rep);

由于某种奇怪的原因,
DOMDocument
返回的字符串忽略换行符(
\n
)结构。

我很好奇它们为什么会这样做。可能是因为我们使用doc.normalize()?@bge0,
doc.normalize
似乎不是原因,因为无论是否使用,行为都是一样的。这几乎就像
replaceAll
模式期望
文字换行符
出现在
replacement
字符串中一样,它只能通过在构造
\n
()的位置包含一个物理回车符来表示;这看起来也绝对不对。
rep = replacement.replaceAll("\\\\n", "\n")
str = str.replaceAll(code, rep);