JAVA-如何在${}中获取字符串

JAVA-如何在${}中获取字符串,java,Java,例如: String str = "bla bla ${foo} ${foo1}"; 如何获得单词“foo”和“foo1” 也许我的字符串是: String str1 = "${foo2} bla bla ${foo3} bla bla"; 如何获取单词“foo2”和“foo3”?您可以使用regex模式和匹配器类。例如: String str = "bla bla ${foo} ${foo1}"; Pattern p = Pattern.compile("\\$\\{([\\w]+)\\}

例如:

String str = "bla bla ${foo} ${foo1}";
如何获得单词“foo”和“foo1”

也许我的字符串是:

String str1 = "${foo2} bla bla ${foo3} bla bla";

如何获取单词“foo2”和“foo3”?

您可以使用regex
模式
匹配器
类。例如:

String str = "bla bla ${foo} ${foo1}";
Pattern p = Pattern.compile("\\$\\{([\\w]+)\\}");
Matcher m = p.matcher(str);
while(m.find()) {
    System.out.println(m.group(1));
}
/* Result:
foo
foo1
 */

这应该行得通

一个解决方案是使用正则表达式。您编写了什么代码?它有什么作用?帮我们重现你的问题。我自己会在谷歌上搜索“括号之间的java提取文本”。这将作为第一个结果出现会有帮助的。谢谢大家,我找到了一些解决办法。使用模式+匹配器。
Pattern p = Pattern.compile("\\${(.*?)\\}");
Matcher m = p.matcher(input);
while(m.find())
{
    //m.group(1) is your string. do what you want
}