Groovy FindAll粗略/喜欢

问题描述 投票:0回答:2

我试图在Groovy中做一些类似于SQL的WHERE NAME LIKE %JOHN%

这是我有的:

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

如果我使用==,这是有效的,但我的代码执行LIKE搜索有问题。

groovy findall
2个回答
1
投票

问题是lookupQuery没有插入正则表达式中。但是,在这种情况下,您实际上不需要使用正则表达式:

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

1
投票

我想你需要:

json.entries.findAll { it.name.toUpperCase() ==~ /.*${lookupQuery.toUpperCase()}.*/ }
© www.soinside.com 2019 - 2024. All rights reserved.