正则验证,取值,匹配器

匹配器,返回匹配到的变量集合

参数:包含{{匹配变量}}的字符串
返回:匹配变量
应用场景:需要从文档中取出相关的模板变量,用于匹配值

/**
 * 匹配器,返回匹配到的变量集合
 * @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);
     }
 }
 
posted @ 2022-08-26 10:22  dlacc  阅读(2)  评论(0)    收藏  举报  来源