Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql regex error #1139 using literal -

I tried running this query:

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

but this returns

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

which seems to me like the - in the character class is not being escaped, and instead read as an invalid range. Is there some other way that it's suppose to be escaped for mysql to be the literal -?

This regex works as expected outside of mysql, https://regex101.com/r/wE8vY5/1.

I came up with an alternative to that regex which is

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

so the question isn't how do I get this to work. The question is why doesn't the first regex work?

Here's a SQL fiddle of the issue, http://sqlfiddle.com/#!9/f8a006/1.

Also, there is no language being used here, query is being run at DB level.

Regex in PHP: http://sandbox.onlinephpfunctions.com/code/10f5fe2939bdbbbebcc986c171a97c0d63d06e55

Regex in JS: https://jsfiddle.net/6ay4zmrb/

like image 208
chris85 Avatar asked Dec 11 '22 21:12

chris85


2 Answers

Just change the order.

SELECT column FROM table WHERE column REGEXP '[^-A-Za-z\']'
like image 193
Avinash Raj Avatar answered Jan 02 '23 16:01

Avinash Raj


@Avinash Raj is correct the - must be first (or last). The \ is not an escape character in POSIX, which is what mysql uses, 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

like image 27
chris85 Avatar answered Jan 02 '23 16:01

chris85