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"
}
}
(?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.*/) ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With