我正在研究表达式值阅读器。在寻找像这样的API而没有找到任何东西后,我决定创建自己的。我创建了一个名为ExpressionMatcher的类。这是它应该如何工作:
private static final String TEMPLATE = "my name is {username} and i am {userage} years old";
private ExpressionMatcher expressionMatcher = new ExpressionMatcher(TEMPLATE);
...
String matias = "my name is Matias and i am 25 years old";
String fede = "my name is Federico and i am 23 years old";
Map<String, String> variables = expressionMatcher.getValuesFromString(matias);
assertEquals("Matias", variables.get("username"));
表达式匹配器的代码是:
public class ExpressionMatcher {
List<String> variableNames = new ArrayList<String>();
private final Pattern expressionPattern;
public ExpressionMatcher(String template) {
// Look for variable names in the string
Pattern pattern = Pattern.compile("\\{(.*?)\\}");
Matcher matcher = pattern.matcher(template);
while(matcher.find()) {
// Add variable names into a list
variableNames.add(matcher.group(1));
}
// replace the variable names with a new regex that will match all incoming expressions and compile a pattern
this.expressionPattern = Pattern.compile(matcher.replaceAll("(.*?)"));
}
public Map<String, String> getValuesFromString(String expression) {
Matcher matcher = this.expressionPattern.matcher(expression);
Map<String, String> variables = new HashMap<String, String>();
if(!matcher.matches()) {
return Collections.emptyMap();
} else {
int i = 0;
while(matcher.find()) {
variables.put(variables.get(i), matcher.group(1));
i++;
}
}
return variables;
}
到目前为止,我已经能够成功获取变量名称,但我无法获得的是纯文本表达式的值。我怎样才能做到这一点? 谢谢,
答案 0 :(得分:0)
没关系,
经过测试后,我解决了这个问题。在getValueFromString方法中,我添加了以下代码:
public Map<String, String> getValuesFromExpression(String expression) {
Matcher matcher = this.expressionPattern.matcher(expression);
Map<String, String> variables = new HashMap<String, String>();
if(!matcher.matches()) {
return Collections.emptyMap();
} else {
// Iterate over the groups
for(int i = 0; i < matcher.groupCount(); i++) {
variables.put(variableNames.get(i), matcher.group(i + 1));
}
}
return variables;
}
我希望至少这对某人有帮助。
答案 1 :(得分:0)
请使用以下代码执行
public static void main(String[] args) {
String template = "Hello {user} request id is {id}. {name} ";
String testString = "Hello ABC , your request id is 1234. Ihab ";
getPlaceholderValues(template, testString);
}
public static Map<String , String> getPlaceholderValues(String template , String testString){
List<String> list = getPatternKeys(template);
String patternFromTemplate = template.replaceAll("\\.", "\\\\.");
for (String item : list){
patternFromTemplate = patternFromTemplate.replace("{"+item+"}", "(.*)");
};
Map<String , String> map = new HashMap<String , String>();
Pattern p = Pattern.compile(patternFromTemplate);
Matcher m = p.matcher(testString);
if (m.matches()) {
for (String item : list){
System.out.println(item +" = "+m.group(list.indexOf(item)+1));
map.put(item, m.group(list.indexOf(item)+1));
}
}
return map;
}
public static List<String> getPatternKeys(String template){
List<String> list = new ArrayList<String>();
Pattern pattern = Pattern.compile("\\{(.*?)\\}");
Matcher matcher = pattern.matcher(template);
while(matcher.find()) {
list.add(matcher.group(1));
}
return list;
}
答案 2 :(得分:0)
String inputStr1 = "Welcome to dataqlo. This is John Doe.";
String template1 = "Welcome to $company. This is $host.";
System.out.println(tpObj.parse(inputStr1,template1));
//Result {host=John Doe, company=dataqlo}
https://github.com/AbhishekSolanki/StringTemplate/blob/master/README.md