如何在Groovy中匹配String和Pattern

Gav*_*iel 3 regex string groovy match gradle

我试图确定一个简单的正则表达式是否匹配Groovy中的字符串.这是我在gradle中的任务.我尝试在网上找到两种不同的方式,但它们都不起作用.它始终打印出"没有发现错误"

task aaa << {
    String stdoutStr = "bla bla errors found:\nhehe Aborting now\n hehe"
    println stdoutStr
    Pattern errorPattern = ~/error/
//  if (errorPattern.matcher(stdoutStr).matches()) {
    if (stdoutStr.matches(errorPattern)) {
        println "ERROR FOUND"
        throw new GradleException("Error in propel: " + stdoutStr)
    } else {
        println "NO ERROR FOUND"
    }
}
Run Code Online (Sandbox Code Playgroud)

cfr*_*ick 9

(?s)忽略.*(DOTALL)的换行符,正则表达式表示完全匹配.所以用==~它作为快捷方式:

if ("bla bla errors found:\nhehe Aborting now\n hehe" ==~ /(?s).*errors.*/) ...
Run Code Online (Sandbox Code Playgroud)