正则验证,取值,匹配器
匹配器,返回匹配到的变量集合
参数:包含{{匹配变量}}的字符串
返回:匹配变量
应用场景:需要从文档中取出相关的模板变量,用于匹配值
/**
* 匹配器,返回匹配到的变量集合
* @param str 包含{{匹配变量}}的字符串
* @return 匹配变量
*/
public void matcherStr(String str){
// 构建取变量正则
Pattern compile = Pattern.compile("(?<=\\{\\{)(.+?)(?=\\}\\})");
Matcher matcher = compile.matcher(str);
Set<String> set = new HashSet<>();
while (matcher.find()) {
System.out.println(matcher.group());
}
}
取springEl表达式中的变量
/**
* 取springEl表达式中的变量
* print: a b c
*/
public void test_el() {
String ruleExpression = "3-(#a+#b)/#c*3";
Pattern pattern = Pattern.compile("#([a-zA-Z0-9_\\-]*)[\\s]?|#([a-zA-Z0-9_\\-]*)$");
Matcher matcher = pattern.matcher(ruleExpression);
while (matcher.find()) {
String rule = matcher.group().trim().substring(1);
System.out.println(rule);
}
}

浙公网安备 33010602011771号