2019-05-03

MySQL - RegEx replacements

-- This may only work with certain versions of MySQL (v8+ possibly)
-- These functions do not work on select statements using a DISTINCT clause

SELECT REGEXP_REPLACE('a b c', 'b', 'X');
SELECT REGEXP_REPLACE('a b c', '[a-z]', 'X');
SELECT REGEXP_REPLACE('a b c 123 cuando ', '[[:alpha:]]', 'X');
SELECT REGEXP_REPLACE('a b c 1987-2087 cuando ', '[[:alpha:]]', '');
SELECT REGEXP_REPLACE('a b c 1987-2087 cuando ', '[[:alpha:]]', '', 1, 0, 'i');


SELECT REGEXP_REPLACE(
author
-- , '[a-z]+' -- Regex Pattern
, '[[:alpha:]]'
, 'X' -- Replace value
, 1   -- Starting position
, 1   -- Occurrence
, 'i' -- Match type (i=case insensitive, c=case sensitive, m=multiple line mode, u=unix-only line endings)
);


No comments:

Post a Comment