Question

I am trying to do something in Groovy similar to SQL's WHERE NAME LIKE %JOHN%

Here is what I have:

response.entries = json.entries.findAll { it.name.toUpperCase() =~ /lookupQuery.toString().toUpperCase()/  }

This is working if I use ==, but something is wrong with my code doing a LIKE search.

Was it helpful?

Solution

The problem is that lookupQuery isn't inserted in the regular expression. In this case however, you don't really need to use regular expressions:

json.entries.findAll { it.name.toUpperCase().contains(lookupQuery.toString().toUpperCase()) }

OTHER TIPS

I think you need:

json.entries.findAll { it.name.toUpperCase() ==~ /.*${lookupQuery.toUpperCase()}.*/ }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top