Java 8 中,当值位于变量中时,使用 replaceAll 替换花括号字符串。

huangapple go评论78阅读模式
英文:

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 = &quot;&lt;xxx.xx.name&gt;xxx.xxx.com:111&lt;/xxx.xx.name&gt;&quot;;
String destination = &quot;\$\\{VAR\\}&quot;;
String source = &quot;xxx.xxx.com:111&quot;;
content = content.replaceAll(source, destination);

Result:

result = {IllegalArgumentException@781} Method threw &#39;java.lang.IllegalArgumentException&#39; exception.
detailMessage = &quot;Illegal group reference&quot;
cause = {IllegalArgumentException@781} &quot;java.lang.IllegalArgumentException: Illegal group reference&quot;
stackTrace = {StackTraceElement[5]@783} 
suppressedExceptions = {Collections$UnmodifiableRandomAccessList@773}  size = 0

But if I do:

content = content.replaceAll(source,&quot;\$\\{VAR\\}&quot;);

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 = &quot;&lt;xxx.xx.name&gt;xxx.xxx.com:111&lt;/xxx.xx.name&gt;&quot;;
String destination = Matcher.quoteReplacement(&quot;${VAR}&quot;);
String source = &quot;xxx.xxx.com:111&quot;;
content = content.replaceAll(source, destination);

这将产生以下结果:

&lt;xxx.xx.name&gt;${VAR}&lt;/xxx.xx.name&gt;

演示链接

英文:

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 = &quot;&lt;xxx.xx.name&gt;xxx.xxx.com:111&lt;/xxx.xx.name&gt;&quot;;
String destination = Matcher.quoteReplacement(&quot;${VAR}&quot;);
String source = &quot;xxx.xxx.com:111&quot;;
content = content.replaceAll(source, destination);

Which results in:

&lt;xxx.xx.name&gt;${VAR}&lt;/xxx.xx.name&gt;

DEMO

huangapple
  • 本文由 发表于 2020年10月1日 04:18:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64145126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定