7 years, 9 months ago Len Nader

Thrilled that MariaDB supports PCRE, one of the best engines out there.
It seems to me that the REGEXP_SUBSTR function is missing an optional parameter. It would be more useful as
where group is 0 by default.
I realize you can do something like
regexp_replace(subject, pattern, '@1')
but that is not super intuitive.
(The @ is actually a double backslash that I don't know how to render on this comment system.)

2 years, 1 month ago Christian Proust

There is one work-around. You can use \K and (?=) to look-ahead and look-behind your subject.

For example:

WITH log(X) AS (
	-- 'over' does not match because 'cute' does not end with a digit
	('Do you know the droid? It is over-cute <3'),
	('I think it is BB-8.'),
	-- 'BB' does not match because it is preceded by 'not'.
	('No it is not BB-8. It is R2-D2.')
-- Search a word preceding by 'is ' and followed by '-\\w*\\d'
SELECT REGEXP_SUBSTR(X, 'is \\K\\w+(?=-\\w*\\d)')
FROM log;
-> ''
-> 'BB'
-> 'R2'
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.