java sql like,如何在java中实现类似'LIKE'运算符的SQL?

"该博客讨论了如何在Java中创建一个比较器,其行为类似于SQL的LIKE操作符。作者提供了几个示例,如`myComparator.like("digital","%ital%")`和`myComparator.like("digital","%gi?a%")`,并解释了期望的匹配结果。文章还提到了使用正则表达式作为可能的实现方式,并给出了相应的Java代码片段。"
摘要由CSDN通过智能技术生成

I need a comparator in java which has the same semantics as the sql 'like' operator.

For example:

myComparator.like("digital","%ital%");

myComparator.like("digital","%gi?a%");

myComparator.like("digital","digi%");

should evaluate to true, and

myComparator.like("digital","%cam%");

myComparator.like("digital","tal%");

should evaluate to false. Any ideas how to implement such a comparator or does anyone know an implementation with the same semantics? Can this be done using a regular expression?

解决方案

.* will match any characters in regular expressions

I think the java syntax would be

"digital".matches(".*ital.*");

And for the single character match just use a single dot.

"digital".matches(".*gi.a.*");

And to match an actual dot, escape it as slash dot

\.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值