【发布时间】:2018-09-28 01:18:22
【问题描述】:
我可以使用Matcher.quoteReplacement.替换美元符号我可以通过添加边界字符来替换单词:
from = "\\b" + from + "\\b";
outString = line.replaceAll(from, to);
但我似乎无法将它们组合起来用美元符号替换单词。
这是一个例子。我正在尝试用“register1”替换“$temp4”(不是$temp40)。
String line = "add, $temp4, $temp40, 42";
String to = "register1";
String from = "$temp4";
String outString;
from = Matcher.quoteReplacement(from);
from = "\\b" + from + "\\b"; //do whole word replacement
outString = line.replaceAll(from, to);
System.out.println(outString);
输出
"add, $temp4, $temp40, 42"
如何让它替换 $temp4 并且只替换 $temp4?
【问题讨论】:
标签: java regex replaceall