mysql中的regexp-查找包含所有关键字的字符串(AND运算符) [英] regexp in mysql - find a string that holds ALL keywords (AND operator)
问题描述
我猜简单的事情让我呆了3个小时,但一直在搜索互联网,却没有找到答案. 我有1个,2个,3个,4个或更多关键字,以及一个在其中搜索关键字的字符串.我正在寻找匹配所有关键字的正则表达式.例如:
I am stuck for 3 hours with i guess simple thing but been searching internet and didnt find the answer. I have 1, 2, 3, 4 or more keywords and a string to search for keywords in it. I am looking for regexp expression that maches ALL the keywords. So for example:
string =这辆车是红色的,有很大的车轮"
string = "this car is red and has big wheels"
关键字:车轮
返回:true
关键字:车轮汽车
返回:true
关键字:汽车为红色
返回:true
关键字:很大
返回:true
关键字:汽车是红色的小
返回:false(字符串中没有小"字)
return: false (there is no 'small' word in string)
我使用这样的mysql查询:
I use such a mysql query:
SELECT name, desc FROM table WHERE CONCAT(name, desc) REGEXP ($keyword1)($keyword2)
但是它返回空字符串.什么是正确的regexp语法?
But it returns empty string. What should be correct regexp syntax?
推荐答案
最简单的解决方案是将AND
从regexp移入SQL:
The easiest solution is to move the AND
out of regexp and into SQL:
SELECT name, desc
FROM table
WHERE CONCAT(name, desc) REGEXP ($keyword1)
AND CONCAT(name, desc) REGEXP ($keyword2)
否则,您的正则表达式将必须在OR
语句的长链中包含所有N!
可能的顺序,并且该长度非常非常快地增长(使用5个关键字,您需要构造120个不同的顺序! )
Otherwise, your regexp would have to include all N!
possible orders in a long chain of OR
statements, and that length grows very, very fast (with 5 keywords, you'd need to construct 120 distinct orderings!)
这篇关于mysql中的regexp-查找包含所有关键字的字符串(AND运算符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!