Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match String with Pattern in Groovy

I am trying to decide whether a simple regular expression matches a string in Groovy. Here's my task in gradle. I tried to match with 2 different ways I found on the net, but neither of them works. It always prints out "NO ERROR FOUND"

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"
    }
}
like image 308
Gavriel Avatar asked Oct 27 '25 03:10

Gavriel


1 Answers

(?s) ignores line breaks for .* (DOTALL) and the regexp there means a full match. so with ==~ as shortcut it's:

if ("bla bla errors found:\nhehe Aborting now\n hehe" ==~ /(?s).*errors.*/) ...
like image 101
cfrick Avatar answered Oct 28 '25 19:10

cfrick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!