MySQL中点字符的全词匹配 [英] Whole word matching with dot characters in MySQL

查看:45
本文介绍了MySQL中点字符的全词匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MySQL 中,当在只需要全词匹配"的文本字段中搜索关键字时,可以使用 REGEXP 和 [[:<:]] 和 [[:>:]] 词边界标记:

In MySQL, when searching for a keyword in a text field where only "whole word match" is desired, one could use REGEXP and the [[:<:]] and [[:>:]] word-boundary markers:

SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]word[[:>:]]"

例如,当我们要查找所有包含europe"的文本字段时,使用

For example, when we want to find all text fields containing "europe", using

SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]europe[[:>:]]"

将返回欧洲地图",而不是欧洲联盟".

would return "europe map", but not "european union".

但是,当目标匹配词包含点字符"时,例如美国",我应该如何提交正确的查询?我尝试了以下查询,但没有一个看起来正确.

However, when the target matching words contains "dot characters", like "u.s.", how should I submit a proper query? I tried the following queries but none of them look correct.

1.

SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]u.s.[[:>:]]"

2.

SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]u[.]s[.][[:>:]]"

3.

SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]u\.s\.[[:>:]]"

当使用双反斜杠转义特殊字符时,如 d'alar'cop 所建议的,它返回空,即使表中有类似美国国会"的内容

When using double backslash to escape special characters, as suggested by d'alar'cop, it returns empty, even though there are something like "u.s. congress" in the table

SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]u\\.s\\.[[:>:]]"

感谢任何建议!

推荐答案

这个正则表达式满足你的要求:

This regex does what you want:

SELECT name
FROM tbl_name
WHERE name REGEXP '([[:blank:][:punct:]]|^)u[.]s[.]([[:punct:][:blank:]]|$)'

这匹配 u.s. 前面有:

  • 空白(空格、制表符等)
  • 标点符号(逗号、括号等)
  • 什么都没有(即在行首)

然后是:

  • 空白(空格、制表符等)
  • 标点符号(逗号、括号等)
  • 什么都没有(即在行尾)

请参阅 SQLFiddle,其中包含涵盖以上几点的边缘情况.

See an SQLFiddle with edge cases covering above points.

这篇关于MySQL中点字符的全词匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆