Umesh Kacha
Umesh Kacha

Reputation: 13666

Regex that works on regex101 does not work in Groovy

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?

Upvotes: 0

Views: 487

Answers (2)

tim_yates
tim_yates

Reputation: 171084

Assuming formula is:

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

I think you just want:

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

Upvotes: 1

Jérôme
Jérôme

Reputation: 1254

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$$

Upvotes: 1

Related Questions