0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Umesh K
  • 13,436
  • 25
  • 87
  • 129

2 Answers2

1

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$$
Jérôme
  • 1,254
  • 2
  • 20
  • 25
1

Assuming formula is:

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

I think you just want:

List result = formula.findAll(/\$\$\w+\$\$/)
tim_yates
  • 167,322
  • 27
  • 342
  • 338