英文:
Java replace curly brackets string using replaceAll when the value is in variable, java 8
问题
我有一个变量:
String content = "<xxx.xx.name>xxx.xxx.com:111</xxx.xx.name>";
String destination = "\$\\{VAR\\}";
String source = "xxx.xxx.com:111";
content = content.replaceAll(source, destination);
结果:
result = {IllegalArgumentException@781} 方法抛出了 'java.lang.IllegalArgumentException' 异常。
detailMessage = "非法的组引用"
cause = {IllegalArgumentException@781} "java.lang.IllegalArgumentException: 非法的组引用"
stackTrace = {StackTraceElement[5]@783}
suppressedExceptions = {Collections$UnmodifiableRandomAccessList@773} size = 0
但是如果我这样做:
```java
content = content.replaceAll(source, "\$\\{VAR\\}");
一切都正常工作。如何模仿或修复这个 replaceAll?
英文:
I have a variable:
String content = "<xxx.xx.name>xxx.xxx.com:111</xxx.xx.name>";
String destination = "\$\\{VAR\\}";
String source = "xxx.xxx.com:111";
content = content.replaceAll(source, destination);
Result:
result = {IllegalArgumentException@781} Method threw 'java.lang.IllegalArgumentException' exception.
detailMessage = "Illegal group reference"
cause = {IllegalArgumentException@781} "java.lang.IllegalArgumentException: Illegal group reference"
stackTrace = {StackTraceElement[5]@783}
suppressedExceptions = {Collections$UnmodifiableRandomAccessList@773} size = 0
But if I do:
content = content.replaceAll(source,"\$\\{VAR\\}");
all is working fine. How can I mimic or fix the replaceAll?
答案1
得分: 4
来自String.replaceAll(String, String)
文档:
> 请注意,替换字符串中的反斜杠(\)和美元符号($)可能会导致结果与将其视为字面替换字符串时的结果不同;
重点在于 可能,根据您的Java版本,这可能会失败(尽管我无法在Java 8、11、12、15甚至早期访问的Java 16中复现您的问题)
您可以使用Matcher.quoteReplacement(String)
来转义替换字符串中的\和$字符,如Java文档中稍后所述:
> 如果需要,可以使用Matcher.quoteReplacement(java.lang.String)来取消这些字符的特殊含义。
因此,将您的代码更改为以下内容(假设您想将内容替换为${VAR}
,而不是\${VAR\}
):
String content = "<xxx.xx.name>xxx.xxx.com:111</xxx.xx.name>";
String destination = Matcher.quoteReplacement("${VAR}");
String source = "xxx.xxx.com:111";
content = content.replaceAll(source, destination);
这将产生以下结果:
<xxx.xx.name>${VAR}</xxx.xx.name>
演示链接
英文:
From the documentation of String.replaceAll(String, String)
:
> Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string;
Emphasis on the may, depending on your Java version this may fail (though I fail to reproduce your problem with Java 8, 11, 12, 15 and even the early access Java 16)
You can use Matcher.quoteReplacement(String)
to escape your \ and $ chars in the replacement string, as described later on in the javadoc:
> Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.
So change your code to this (assuming you want to replace the contents with ${VAR}
and not with \${VAR\}
):
String content = "<xxx.xx.name>xxx.xxx.com:111</xxx.xx.name>";
String destination = Matcher.quoteReplacement("${VAR}");
String source = "xxx.xxx.com:111";
content = content.replaceAll(source, destination);
Which results in:
<xxx.xx.name>${VAR}</xxx.xx.name>
DEMO
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论