2017-03-15 32 views
0

嗨,我有以下的正則表達式正則表達式不是在Groovy

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

以上應該理想地打印$$值1 $$

現在

這個表達式爲以下字符串工作正常上regex101.com但同樣正常工作在Groovy上不起作用。理想情況下,它應該使用匹配API找到兩個組$ $$ 1 $$和$$ value2 $$,但它不會。

正則表達式有什麼問題嗎?請指導。提前致謝。

+0

嗨@collinD請我最新的編輯。 – u449355

+0

顯示您的Groovy或Java代碼。請注意,在regex101下工作的正則表達式不必在Java中工作,因爲regex101不支持Java正則表達式語法。 –

+0

這是Groovy,而不是Java –

回答

1

假設formula是:

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

我覺得你只是想:

List result = formula.findAll(/\$\$\w+\$\$/) 
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$$