【问题标题】:StrSubstitutor issue with $$ 的 StrSubstitutor 问题
【发布时间】:2017-08-31 18:24:56
【问题描述】:

我正在尝试像这样使用 org.apache.commons.lang3.text.StrSubstitutor 替换字符串

      Map<String, String> valuesMap = new HashMap<>();
      valuesMap.put("price", "40");
      valuesMap.put("product", "Shirt");
      String templateString = "I paid total ${{price}} for the product {{product}}.";
      StrSubstitutor sub = new StrSubstitutor(valuesMap, "{{", "}}");
      String resolvedString = sub.replace(templateString);
      System.out.println(resolvedString);

然后我得到以下输出

我为产品衬衫支付了 {{price}} 的总费用。

而不是

我为 Shirt 产品总共支付了 40 美元。

我认为 $ 符号必须与 StrSubstitutor 中处理正则表达式的方式相冲突。

我错过了什么吗?如果没有,我怎样才能得到预期的解决方案?

【问题讨论】:

    标签: java apache-commons substitution


    【解决方案1】:

    也找到了另一种解决方案,如果我更改转义字符,那么它会起作用

    sub.setEscapeChar('^');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多