在MySQL中为一列中的重复单词匹配正则表达式

在MySQL中为一列中的重复单词匹配正则表达式,mysql,sql,regex,Mysql,Sql,Regex,我有个疑问。我使用mysql作为数据库。 我想使用正则表达式来匹配我期望的结果 桌子是 table A ---------------------------------- | ID | Description | ---------------------------------- | 1 | new 2 new 2 new 2 new | | 2 | new 2 new 2 new | | 3 | new 2

我有个疑问。我使用mysql作为数据库。 我想使用正则表达式来匹配我期望的结果 桌子是

table A

----------------------------------
|   ID  | Description            |
----------------------------------
|   1   |  new 2 new 2 new 2 new |
|   2   |   new 2 new 2 new      |
|   3   |   new 2                |
|   4   |   2 new 2new           |
我期望的结果

---------------------------------
|   ID  | Description           |
---------------------------------
|   2   |   new 2 new 2 new     |
|   4   |   2 new 2new          |
到目前为止我尝试过的查询:

SELECT * FROM a WHERE (description REGEXP '([^2][^0..9]])2( [^2][^0..9])([^2][^0..9]])2( [^2][^0..9])')


有人能帮我解决这个问题吗:(?

你的正则表达式没有做你认为它做的事情(尽管我不能完全猜出你认为它做什么…)

部分正则表达式的翻译:

([^2][^0..9]])2
指:

(         # Start a group
 [^2]     # Match one character except "2"
 [^0..9]  # Match one character except "0", "." or "9"
 ]        # Match "]"
)         # End of group
2         # Match "2"

正如@Tim Pietzcker所指出的,正则表达式的作用与您可能认为的不同。如果我理解正确,我相信您正在寻找以下正则表达式。它分别返回ID
2
4

^[^2]*2[^2]*2[^2]*$
您的SQL查询将是:

SELECT * FROM a WHERE (description REGEXP '^[^2]*2[^2]*2[^2]*$')

输出中有什么特别之处?为什么不匹配第1行?您能描述一下您希望正则表达式执行的操作吗?为什么您希望它返回该结果?