shadow
shadow

Reputation: 26

Regexp invalid character length

This MySQL query works:

SELECT p.slug FROM products AS p
WHERE p.slug REGEXP '[^a-zA-Z0-9_-]'

but this one does not work:

SELECT p.slug FROM products AS p
WHERE p.slug REGEXP '[^a-zA-Z0-9_-(]'

neither this one works:

SELECT p.slug FROM products AS p
WHERE p.slug REGEXP '[^a-zA-Z0-9_-\(]'

Upvotes: 0

Views: 89

Answers (2)

Shanteshwar Inde
Shanteshwar Inde

Reputation: 1466

you need to use \\ to escape for that special character

SELECT p.slug FROM products AS p WHERE p.slug REGEXP '[^a-zA-Z0-9_-\\(]'

For reference please read doc

EDIT: use \\ to escape any special character in your expression. like in above query - is special character.

Upvotes: 4

agold
agold

Reputation: 6276

You need to escape special characters (parentheses and dash) with a double backslash (\\):

SELECT p.slug FROM products AS p
WHERE p.slug REGEXP '[^a-zA-Z0-9_\\-\\(]'

The dash doesn't need to be escaped at the beginning or at the end since otherwise it is interpreted as being part of a range (e.g. a-z):

'[^a-zA-Z0-9_\\(-]'

Upvotes: 2

Related Questions