Mysql 正则表达式错误 #1139 使用文字 -

Mysql regex error #1139 using literal -

我试过运行宁此查询:

SELECT column FROM table WHERE column REGEXP '[^A-Za-z\-\']'

但是这个returns

#1139 - Got error 'invalid character range' from regexp

在我看来,字符 class 中的 - 没有被转义,而是被读取为无效范围。是否有其他方法可以将 mysql 转义为文字 -?

此正则表达式在 mysql、https://regex101.com/r/wE8vY5/1.

之外按预期工作

我想出了一个替代正则表达式的方法

SELECT column FROM table WHERE column NOT REGEXP '([:alpha:]|-|\')'

所以问题不是我如何让它工作。问题是为什么第一个正则表达式不起作用?

这里有一个SQLfiddle的问题,http://sqlfiddle.com/#!9/f8a006/1

此外,这里没有使用任何语言,查询是 运行 在数据库级别。

PHP 中的正则表达式:http://sandbox.onlinephpfunctions.com/code/10f5fe2939bdbbbebcc986c171a97c0d63d06e55

JS 中的正则表达式:https://jsfiddle.net/6ay4zmrb/

只需更改顺序即可。

SELECT column FROM table WHERE column REGEXP '[^-A-Za-z\']'

@Avinash Raj 是正确的 - 必须是第一个(或最后一个)。 \ 不是 POSIX 中的转义字符,mysql 使用的是 https://dev.mysql.com/doc/refman/5.1/en/regexp.html.

One key syntactic difference is that the backslash is NOT a metacharacter in a POSIX bracket expression.

-http://www.regular-expressions.info/posixbrackets.html

What special characters must be escaped in regular expressions?

Inside character classes, the backslash is a literal character in POSIX regular expressions. You cannot use it to escape anything. You have to use "clever placement" if you want to include character class metacharacters as literals. Put the ^ anywhere except at the start, the ] at the start, and the - at the start or the end of the character class to match these literally