适用于regex101的正则表达式在Groovy中不起作用

时间:2017-03-15 13:50:02

标签: regex groovy

我有以下正则表达式

tom

上面应该理想地打印def formula = math:min(math:round($$value1$$ * $$value2$$) ) def m = formula =~ /\$\$\w+\$\$/ println m.group(1)

现在这个以下字符串的正则表达式在regex101.com上运行正常,但同样不适用于Groovy。理想情况下,它应该使用Matcher API找到两组$$ value1 $$和$$ value2 $$,但事实并非如此。

这个正则表达式有什么问题吗?

2 个答案:

答案 0 :(得分:1)

我在java中试过你的正则表达式,如果我在正则表达式的开头和结尾删除了/,它对我有用。

public class RegexTest {

  public static void main(String[] args) {
    String regex = "\\$\\$\\w+\\$\\$";
    String test = "math:min(math:round($$value1$$ * $$value2$$) ) ";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(test);
    while (matcher.find()){
      System.out.println(matcher.group());
    }    
  }
}

它返回

$$value1$$
$$value2$$

答案 1 :(得分:1)

假设formula是:

def formula = 'math:min(math:round($$value1$$ * $$value2$$) )'

我想你只是想:

List result = formula.findAll(/\$\$\w+\$\$/)
相关问题