I’m looking for an SQL statement that will return only rows of my table whose Name field contains special characters (excluding underscores).
I’ve tried:
SELECT * FROM 'table' WHERE Name REGEXP '^[!#$%&()*+,\-./:;<=>?@[\\\]^`{|}~]+$'
But no dice, this returns an empty result set (despite there being rows I specifically added with Name fields containing %, $, and # characters).
The first problem seems to be is the
^and$signs (Mike C summarized it quicker than I did why…)But I see escaping problems too: all special characters that mean something in regexp should be
escapedspecially placed in the[], so[,],^,-Here is a question about how to escape special characters inside character groups in MySQL regexes.
Conclusion detailed in the regex documentation:
EDIT
Here is an SQL fiddle about some interesting regexes regarding the
]characterDDL:
create table txt (
txt varchar(200)
);
Queries:
Naive approach to match a group of
[and]chars. Syntactically OK, but the group is the single[char, and it matches multiple]chars afterwards.Escaped -> same ???
Double escape -> doesn’t work, group is now a
[and a\Swapping the closing bracket with the opening one inside the group. This is the weirdest regex I ever wrote – to this point…
I will get killed by such a (totally valid!) regex in a weird nightmare, I think: