简体   繁体   English

MySQL查询使用REGEXP找不到匹配的字符串

[英]MySQL query to find matching string using REGEXP not working

I am using MySQL 5.5 . 我正在使用MySQL 5.5

I have a table named nutritions , having a column serving_data with text datatype. 我有一个名为nutritions的表,有一个serving_data列和text数据类型。

Some of the values in serving_data column are like: serving_data列中的一些值如下:

[{"label":"1 3\/4 cups","unit":"3\/4 cups"},{"label":"1 cups","unit":"3\/4 cups"},{"label":"1 container (7 cups ea.)","unit":"3\/4 cups"}]

Now, I want to find records containing serving_data like 1 3\\/4 cups . 现在,我想找到包含serving_data记录,如1 3\\/4 cups

For that I've made a query, 为此,我提出了一个问题,

SELECT id,`name`,`nutrition_data`,`serving_data`
FROM `nutritions` WHERE serving_data REGEXP '(\d\s\\\D\d\scup)+';

But is seems not working. 但似乎行不通。

Also I've tried 我也试过了

SELECT id,`name`,`nutrition_data`,`serving_data`
FROM `nutritions` WHERE serving_data REGEXP '/(\d\s\\\D\d\scup)+/g';

If I use the same pattern in http://regexr.com/ then it seems matching. 如果我在http://regexr.com/中使用相同的模式,那么它似乎匹配。

Can anyone help me? 谁能帮我?

Note that in MySQL regex, you cannot use shorthand classes like \\d , \\D or \\s , replace them with [0-9] , [^0-9] and [[:space:]] respectively. 请注意,在MySQL正则表达式中,您不能使用\\d\\D\\s等速记类,分别用[0-9][^0-9][[:space:]]替换它们。

You may use 你可以用

REGEXP '[0-9]+[[:space:]][0-9]+\\\\/[0-9]+[[:space:]]+cup'

See the regex demo (note that in general, regex101.com does not support MySQL regex flavor, but the PCRE option supports the POSIX character classes like [:digit:] , [:space:] , so it is only used for a demo here, not as a proof it works with MySQL REGEXP ). 请参阅正则表达式演示 (请注意,一般情况下,regex101.com不支持MySQL正则表达式,但PCRE选项支持POSIX字符类,如[:digit:][:space:] ,因此它仅用于演示在这里,不作为它与MySQL REGEXP一起使用的证据)。

Pattern details : 图案细节

  • [0-9]+ - 1 or more digits [0-9]+ - 1位或更多位数
  • [[:space:]] - a whitespace [[:space:]] - 一个空格
  • [0-9]+ - 1 or more digits [0-9]+ - 1位或更多位数
  • \\\\\\\\/ - a literal \\/ char sequence \\\\\\\\/ - 文字\\/字符序列
  • [0-9]+[[:space:]]+cup - 1 or more digits, 1 or more whitespaces, cup . [0-9]+[[:space:]]+cup - 1个或更多个数字,1个或更多个空格, cup

Note that you may precise the word cup with a word boundary, add a [[:>:]] pattern after it to match a cup as a whole word. 请注意,您可以使用单词边界精确单词cup ,在其后添加[[:>:]]模式以匹配作为整个单词的cup

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

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