Mysql
 SQL >> Datenbank >  >> RDS >> Mysql

Hex-Zeichen in MySQL-REGEXP korrekt verwenden

Das hier matcht ebenfalls:

SELECT CONVERT('a' USING BINARY) REGEXP '[1-\x]';

Der Grund: \x wird als x interpretiert, und a liegt zwischen 1 und x. Die übrigen Zeichen der Regex sind hier irrelevant, weil sie bereits im Bereich [1-x] liegen.

SELECT CONVERT('0' USING BINARY) REGEXP '[\x61-\x61]'; -- scheitert: 0 < 1
SELECT CONVERT('1' USING BINARY) REGEXP '[\x61-\x61]'; -- trifft: in [1-x]
SELECT CONVERT('w' USING BINARY) REGEXP '[\x61-\x61]'; -- trifft: in [1-x]
SELECT CONVERT('y' USING BINARY) REGEXP '[\x61-\x61]'; -- scheitert: y > x

Was Sie stattdessen tun können

MySQL kennt keine \xNN-Notation in regulären Ausdrücken. Nutzen Sie HEX() für die Umwandlung:

SELECT HEX('a');   -- 61

Alternativ mit CHAR() und 8.0 auch REGEXP_LIKE():

SELECT 'abc' REGEXP BINARY '^[[:xdigit:]]+$';

Mit ICU (MySQL 8.0) sind Unicode-Eigenschaften möglich, etwa REGEXP_LIKE(spalte, '[[:ascii:]]').