Regex that works on regex101 does not work in Groovy

て烟熏妆下的殇ゞ 提交于 2020-01-17 06:45:40

问题


I have the following regex

def formula = math:min(math:round($$value1$$ * $$value2$$) ) 
def m = formula =~ /\$\$\w+\$\$/
println m.group(1)

Above should ideally print $$value1$$.

Now this regex for the following string works fine on regex101.com but same does not work on Groovy. Ideally it should find two groups $$value1$$ and $$value2$$ using Matcher API, but it does not.

Is there anything wrong in this regex?


回答1:


Assuming formula is:

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

I think you just want:

List result = formula.findAll(/\$\$\w+\$\$/)



回答2:


I tried your regex in java and it works for me if i remove the / at the beginning and the end of the regex.

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());
    }    
  }
}

it returns

$$value1$$
$$value2$$


来源:https://stackoverflow.com/questions/42811719/regex-that-works-on-regex101-does-not-work-in-groovy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!