©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The REGEXP_SUBSTR SQL Function in MySQL and MariaDB - Substring by regular expression

REGEXP_SUBSTRSyntax:REGEXP_SUBSTR(String, Pattern [, Position] [, Occurrence] [, Match type])
Return value:CHAR
Function type:Regular Expressions
Function Description

The REGEXP_SUBSTR() function returns the portion of "String" that matches the regular expression of "Pattern".

If the "Pattern" is not found, the function returns an empty string on MariaDB 10.3, while on MySQL 8.0 the result is NULL.

The function respects the case-sensitive rules of the effective sorting.

If the value of "String" or "Pattern" is NULL, the return value of the REGEXP_SUBSTR() function is also NULL.


In MySQL, REGEXP_INSTR() accepts the following optional arguments: These options are not available in MariaDB.

"Position": The position in "String" at which to start the search. The default is 1 if not specified.

"Occurrence": What occurrence of a match to search for. The default is 1 if not specified.

"Match type": A string specifying how the match is performed:


TypeDescription
cCase sensitive.
iCase-insensitive match.
mMultiline mode. Line terminators within the string are recognized. The default behavior is to match line terminators only at the beginning and end of "String".
nA point "." is interpreted as a line terminator: Default.
uUnix-only line endings. Only the newline character is recognized as a newline by the ".", ",", "^" and "$" match operators.

If characters are specified within "Match type" that result in conflicting options, the character on the right takes precedence.

Examples of the REGEXP_SUBSTR() function on MariaDB 10.3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

SELECT REGEXP_SUBSTR('test-abc-abc-abc''Test');
SELECT REGEXP_SUBSTR(cast('test-abc-abc-abc' as char character set utf8mb4), cast('Test' as char character set utf8mb4));

SELECT REGEXP_SUBSTR('test-abc-ABC-aBc''(?i)abc');
SELECT REGEXP_SUBSTR('test-abc-ABC-aBc''(?i)ABC');
SELECT REGEXP_SUBSTR('test-abc-ABC-aBc''(?-i)ABC');

SELECT REGEXP_SUBSTR(null'abc');
SELECT REGEXP_SUBSTR('test-aa-abc'null);

REGEXP_SUBSTR('test-abc-abc-abc', 'Test')
varchar(16) BINARY
 
REGEXP_SUBSTR(cast('test-abc-abc-abc' as char character set utf8mb4), cast('Test' as char character set utf8mb4))
varchar(16) BINARY
test
REGEXP_SUBSTR('test-abc-ABC-aBc', '(?i)abc')
varchar(16) BINARY
abc
REGEXP_SUBSTR('test-abc-ABC-aBc', '(?i)ABC')
varchar(16) BINARY
abc
REGEXP_SUBSTR('test-abc-ABC-aBc', '(?-i)ABC')
varchar(16) BINARY
ABC
REGEXP_SUBSTR(null, 'abc')
varchar(0) BINARY
NULL
REGEXP_SUBSTR('test-aa-abc', null)
varchar(11) BINARY
NULL

Examples of the REGEXP_SUBSTR() function on MySQL 8.0

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13

SELECT REGEXP_SUBSTR('test-abc-abc-abc''Test');
SELECT REGEXP_SUBSTR(cast('test-abc-abc-abc' as char character set utf8mb4), cast('Test' as char character set utf8mb4));

SELECT REGEXP_SUBSTR('test-abc-ABC-aBc''(?i)abc');
SELECT REGEXP_SUBSTR('test-abc-ABC-aBc''(?i)abc', 7);
SELECT REGEXP_SUBSTR('test-abc-ABC-aBc''(?i)abc', 1, 2);
SELECT REGEXP_SUBSTR('test-abc-ABC-aBc''(?i)abc', 1, 3);
SELECT REGEXP_SUBSTR('test-abc-ABC-aBc''abc', 1, 2, 'i');

SELECT REGEXP_SUBSTR(null'abc');
SELECT REGEXP_SUBSTR('test-aa-abc'null);

REGEXP_SUBSTR('test-abc-abc-abc', 'Test')
varchar(16) BINARY
NULL
REGEXP_SUBSTR(cast('test-abc-abc-abc' as char character set utf8mb4), cast('Test' as char character set utf8mb4))
varchar(16) BINARY
test
REGEXP_SUBSTR('test-abc-ABC-aBc', '(?i)abc')
varchar(16) BINARY
abc
REGEXP_SUBSTR('test-abc-ABC-aBc', '(?i)abc', 7)
varchar(16) BINARY
ABC
REGEXP_SUBSTR('test-abc-ABC-aBc', '(?i)abc', 1, 2)
varchar(16) BINARY
ABC
REGEXP_SUBSTR('test-abc-ABC-aBc', '(?i)abc', 1, 3)
varchar(16) BINARY
aBc
REGEXP_SUBSTR('test-abc-ABC-aBc', 'abc', 1, 2, 'i')
varchar(16) BINARY
ABC
REGEXP_SUBSTR(null, 'abc')
varchar(0) BINARY
NULL
REGEXP_SUBSTR('test-aa-abc', null)
varchar(11) BINARY
NULL
The examples were created with the MyWAY SQL manager: Download

How to use the REGEXP_SUBSTR() function in MySQL and MariaDB databases

In MySQL and MariaDB, the REGEXP_SUBSTR() function is used to extract substrings from a string based on a specified regular expression pattern. It is thus possible to search for patterns within a character string and retrieve the matching sub-character strings. The function can perform pattern-based substring extractions and can be useful for tasks such as extracting specific parts of a string, parsing text data, and manipulating data based on pattern matching.
REGEXP_INSTRREGEXP_LIKEREGEXP_REPLACEREGEXP_SUBSTRREPEATREPLACEREVERSE

Further MySQL und MariaDB SQL Regular Expressions

REGEXPString [NOT] REGEXP Pattern
More about REGEXP Function

REGEXP_INSTRREGEXP_INSTR(String, Pattern [, Position] [, Occurrence] [, Return option] [, Match type])
More about REGEXP_INSTR Function

REGEXP_LIKEREGEXP_LIKE(String, Pattern [, Match type])
More about REGEXP_LIKE Function

REGEXP_REPLACEREGEXP_REPLACE(String, Pattern, Replace by [, Position] [, Occurrence] [, Match type])
More about REGEXP_REPLACE Function

RLIKEString [NOT] RLIKE Pattern
More about RLIKE Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the REGEXP_SUBSTR SQL function: mysql.com and mariadb.com
Updated: 2023-09-24ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer