![](/img/trans.png)
[英]How can I use RLIKE to effectively combine LIKE and IN, *and* escape regex characters where needed?
[英]How to use like/rlike to filter out string with space on either side
我试图写一个查询来查找除了没有空格的字符串。 这意味着“ xx applexx”,“ xxapple xx”和“ xx apple xx”是不可接受的,但应查询“ xxapplexx”。
我试过了
select a
from b
where b.c not rlike '(an apple | an apple)'
但是这种表达似乎是错误的。
更多示例:假设我们在数据库中有4条记录:
Name Data
a I have an appleYo
b I havean apple Yo
c I have an apple Yo
d I havean appleYo
预期结果应为:
Name Data
d I havean appleYo
您可以使用
where b.c rlike '\\Sapple\\S'
或者,如果您想从匹配中“排除”非工作空间字符(对于rlike
场景,这不建议使用,而是使用replace操作)
where b.c rlike '(?<=\\S)apple(?=\\S)'
参见regex演示 。
细节
\\S
除空格以外的任何字符 (?<=\\S)
-向后的正向查找,它需要除当前位置之前的空格以外的任何字符 (?=\\S)
-正向超前,它需要在当前位置之后立即添加除空白以外的任何字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.