Java replaceAll by函数返回值[gwt]

Java replaceAll by函数返回值[gwt],java,gwt,Java,Gwt,是否可以执行以下操作: calculate(String match) { if (match.equals("expr")) return "no time"; else return "some time"; } String text = "You have {expr} or {other} left"; text = text.replaceAll( "{(.+)}", calculate(match) ); 在那里,花括号内的值,如“exp

是否可以执行以下操作:

calculate(String match) {
    if (match.equals("expr"))
      return "no time";
    else
      return "some time";
}

String text = "You have {expr} or {other} left";
text = text.replaceAll( "{(.+)}", calculate(match) );
在那里,花括号内的值,如“expr”,将以某种方式在函数计算(“expr”)中处理,结果将用作替换?结果字符串应该如下所示

"You have no time or some time left"

我知道类似的东西在javascript中是可能的,但我不知道如何在GWT中实现这一点这不是您想要的,但它应该会给出您想要的结果:

public static String calculate( String text )
{
    String regex = "[{][^{]+[}]", replacement = "";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(text);
    while ( m.find() )
    {
        String match = m.group();

        if ( "{expr}".equals(match) ) replacement = "X";
        else replacement = "Y";

        text = text.replaceFirst(regex, replacement);
    }

    return text;
}
然后使用它:

String text = "You have {expr} or {other} left";
System.out.println(calculate(text));
你可以用。在您的示例中,这类似于:

String text = "You have %s left";
String resultText = String.format(text, calculate(match));
根据calculate返回的结果,resultText是以下两种情况之一:

"You have some time left"
"You have no time left"
%s
用于字符串。在提供的链接中,您可以看到许多其他可用选项

您还可以在单个字符串中使用多个
%-选项

String text = "You have %s left to do the following task: %s";
String resultText = String.format(text, calculate(match)), "Programming in Java");
// OR
String resultText2 = String.format(text, new String[]{ calculate(match), "Programming in Java" });

是否可以将
text
作为额外参数添加到方法
calculate
-然后在该方法中去掉表达式,处理它并返回结果?我不确定是否理解。我对我的问题进行了编辑,以便更清楚地表达我的期望,但无论如何,谢谢你,我认为这不是我期望的。我编辑了我的问题,因为“match”应该是找到的“expr”或“other”。很抱歉造成这种混乱