Groovy web console

subscribe to the feed Subscribe
to this
site
Groovy Null Check failing (via #groovywebconsole)
tweet this snippet Tweet
this
script

Groovy Null Check failing

Published 5 months ago by Hardik Modha with tags Groovy Null Check
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import java.util.regex.Matcher
import java.util.regex.Pattern 
Matcher testMatcher() {
    String str = 'HELLO["WORLD"]'
    Matcher matcher = Pattern.compile('\\[([^]]+)\\]').matcher(str)
    return matcher.find() ? matcher : null
}

Matcher matcher = testMatcher()

if (matcher) {
   println "Not null"
} else {
   println "Null"
}