将两个$之间的子字符串替换为其他字符串



我需要使用JAVA 将两美元($(之间的子字符串替换为其他字符串

String original = "my original string $replace_string$";
String toReplace = "test";

现在我需要以下输出

my original string test

使用一些正则表达式模式,如

original.replace(some_regex, toReplace)

使用正则表达式:\$.+?\$

String mystr = original.replaceAll("\$.+?\$",toReplace);
  • $$完全匹配
  • .+?勉强匹配$之后的每个字符
  • $$完全匹配

最新更新