我正在尝试查找定义明确的字符串的值。例如:
String template = "From \"{1}\" to \"{2}\".";
String input = "From \"A\" to \"B\".";
此处的输出将是一个字符串数组,其值为["A", "B"]
。
最佳答案
使用正则表达式
。
您的模板可以用正则表达式表示:/From "(.*)"to "(.*)"\./
在java中,你可以这样写:
Pattern regex = Pattern.compile("From \"(.*)\" to \"(.*)\"\\.");
Matcher matcher = regex.matcher("From \"A\" to \"B\".");
if(matcher.find())
{
String a = matcher.group(1);
String b = matcher.group(2);
return new String[]{a,b};
}
关于java - 从模板字符串中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52776130/