MariaDB REGEXP_SUBSTR() Function

In MariaDB, the REGEXP_SUBSTR() function returns the substring matching the specified regular expression from a string.

MariaDB REGEXP_SUBSTR() Syntax

Here is the syntax for the MariaDB REGEXP_SUBSTR() function:

REGEXP_SUBSTR(str, regexp)

Parameters

str

Required. a string.

regexp

Required. regular expression.

Note that at the time of writing, MariaDB’s version accepts fewer parameters than MySQL’s REGEXP_SUBSTR(). MySQL REGEXP_SUBSTR() allows you to provide parameters for where to start the search, which matches to search for, and ways to refine the regular expression.

If you provide the wrong number of parameters, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'REGEXP_SUBSTR'.

Return value

The REGEXP_SUBSTR() function searches for a substring matching the regular expression regexp in str and returns it. Returns NULL if there is no match.

REGEXP_SUBSTR() will return NULL if str or regexp is NULL.

MariaDB REGEXP_SUBSTR() Examples

The following statement is used to find the first group of consecutive numbers in '123 abc 456 def'.

SELECT REGEXP_SUBSTR('123 abc 456 def', '\\d+');

Output:

+------------------------------------------+
| REGEXP_SUBSTR('123 abc 456 def', '\\d+') |
+------------------------------------------+
| 123                                      |
+------------------------------------------+

In this case, there are two matches: 123 and 456, but REGEXP_SUBSTR() returns the first match 123.

Conclusion

In MariaDB, the REGEXP_SUBSTR() function returns the substring matching the specified regular expression from a string.