检查字符串中是否包含超过5位的数字

Pel*_*lin 2 mysql sql

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

询问;

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

样本数据

Some text here 123456 (MATCH)
S0m3 t3xt h3r3
Some text 123 here 345
98765 Some text here (MATCH)
Some12345Text Around (MATCH)
Run Code Online (Sandbox Code Playgroud)

所需输出

3 (Some text here 123456, 98765 Some text here, Some12345Text Around)
Run Code Online (Sandbox Code Playgroud)

MySQL 查询中的正则表达式有什么特定规则吗?

Mic*_*ski 5

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

手册中描述了可用的正则表达式语法

因此,您可以使用以下任一形式:

 title REGEXP '[0-9]{5,}'
 title REGEXP '[[:digit:]]{5,}'
Run Code Online (Sandbox Code Playgroud)

例子:

不匹配:

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

> SELECT '1X345' REGEXP '[0-9]{5,}';
+--------------------------------+
| '123' REGEXP '[0-9]{5,}'       |
+--------------------------------+
|                              0 |
+--------------------------------+
Run Code Online (Sandbox Code Playgroud)

匹配示例:

> 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)
Run Code Online (Sandbox Code Playgroud)