简体   繁体   English

MySQL REGEXP 数字完全匹配不起作用

[英]MySQL REGEXP digit exact match not working

I have this query:我有这个查询:

SELECT foo FROM bar WHERE foo REGEXP '[[:digit:]]{10}';

Which is returning bar rows where foo is longer in number of characters than 10.这将返回foo的字符数超过 10 个的bar行。

Same happens if I try with [0-9]{10} .如果我尝试使用[0-9]{10}也会发生同样的情况。 It's like it's not taking into account the {10} to match exactly those rows where foo contains only 10 digits.就像它没有考虑到{10}来完全匹配 foo 仅包含 10 位数字的那些行。

The expected regular expression should be matching values like:预期的正则表达式应该匹配如下值:

1231231231

Only digits, 10 characters, but isn't.只有数字,10 个字符,但不是。

foo is a string column. foo是一个字符串列。 I'm using mysql Ver 14.14 Distrib 5.7.26, for Linux (x86_64).我正在使用 mysql Ver 14.14 Distrib 5.7.26,用于 Linux (x86_64)。

Place anchors around your regex pattern.在您的正则表达式模式周围放置锚点。 Also, I prefer using [0-9] to represent any digit, because it is more portable to other regex flavors.此外,我更喜欢使用[0-9]来表示任何数字,因为它更易于移植到其他正则表达式风格。

SELECT foo
FROM bar
WHERE foo REGEXP '^[0-9]{10}$';

Note that I also placed ^ and $ anchors around the pattern, to ensure that the entire foo column must be ten digits.请注意,我还在模式周围放置了^$锚点,以确保整个foo列必须是十位数字。

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

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