简体   繁体   English

使用正则表达式匹配MYSQL中句子中的单词

[英]match a word from a sentence in MYSQL using regex

How can I match a word in a sentence from a column using regex? 如何使用正则表达式匹配列中句子中的单词? I tried: 我试过了:

"select * from table where column regex '/\b".$text."\b/i'"

but that didn't work. 但这没用。

MySQL is particularly bad about stuff like this. MySQL对于这样的事情特别糟糕。 I'd recommend using MATCH AGAINST syntax http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html or using something like SOLR if you're going to be doing this in volume. 我建议使用MATCH AGAINST语法http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html或使用SOLR之类的方法(如果要批量执行)。

Try this: 尝试这个:

"SELECT * FROM table WHERE column REGEXP '[[:<:]]" . $text . "[[:>:]]'"

You should make sure that any characters that could be interpreted as special characters in $text are properly escaped. 您应该确保正确地转义了可以在$ text中解释为特殊字符的所有字符。 You should also ensure that you do not get an SQL injection. 您还应该确保没有得到SQL注入。

I think you want the LIKE clause! 我认为您想要LIKE子句!

SELECT * FROM table WHERE column LIKE '%value%'

Also please read UltimateBrent's answer as full text search is a very good point. 另外,请阅读UltimateBrent的答案,因为全文搜索非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM