mysql-检查字符串是否包含超过5位数字

时间:2018-08-17 20:33:21

标签: mysql sql mysqli

我正在尝试建立一个查询,以检查字符串是否至少包含5个以上连续数字。

查询;

SELECT count(id) as gV FROM  someTable WHERE ... title REGEXP '(\d{5,})' order by id limit 0,10

样本数据

Some text here 123456 (MATCH)
S0m3 t3xt h3r3
Some text 123 here 345
98765 Some text here (MATCH)
Some12345Text Around (MATCH)

所需的输出

3 (Some text here 123456, 98765 Some text here, Some12345Text Around)

mysql查询中是否有针对正则表达式的特定规则?

1 个答案:

答案 0 :(得分:4)

MySQL的正则表达式引擎未实现\d“数字”表达式,但您可以将其表示为[0-9]之类的字符类范围或特殊字符类[[:digit:]] 。您尝试使用的格式支持大括号重复语法{5,}

可用的正则表达式语法is described in the manual

因此您可以使用以下两种形式之一:

 title REGEXP '[0-9]{5,}'
 title REGEXP '[[:digit:]]{5,}'

示例:

不匹配:

> SELECT '123' REGEXP '[[:digit:]]{5,}';
+--------------------------------+
| '123' REGEXP '[[:digit:]]{5,}' |
+--------------------------------+
|                              0 |
+--------------------------------+

> SELECT '1X345' REGEXP '[0-9]{5,}';
+--------------------------------+
| '123' REGEXP '[0-9]{5,}'       |
+--------------------------------+
|                              0 |
+--------------------------------+

匹配示例:

> SELECT '98765 Some text here' REGEXP '[[:digit:]]{5,}';
+-------------------------------------------------+
| '98765 Some text here' REGEXP '[[:digit:]]{5,}' |
+-------------------------------------------------+
|                                               1 |
+-------------------------------------------------+

> SELECT 'Some text here 123456' REGEXP '[0-9]{5,}';
+--------------------------------------------+
| 'Some text here 123456' REGEXP '[0-9]{5,}' |
+--------------------------------------------+
|                                          1 |
+--------------------------------------------+
1 row in set (0.00 sec)