REGEXP_ SUBSTR
On this page
Searches a string for a regular expression pattern and returns the matching substring.
REGEXP_ supports extended and advanced regular expression formats.regexp_ to specify which format you want to use.
Syntax
REGEXP_SUBSTR (<source_string>, <pattern> [, <position> [, <occurrence> [, <parameters> ]]])Arguments
-
source_
string: A string expression, such as a column name, to be searched. -
pattern: A string literal representing a regular expression.
-
position: Optional if the
occurrenceandparametersarguments are not specified.A positive integer indicating the position within
source_to begin searching.string The position is based on the number of characters, not bytes, so that multibyte characters are counted as single characters. The default is 1.If positionis less than1, the search begins at the first character ofsource_.string If positionis greater than the number of characters insource_, the result isstring NULL. -
occurrence: Optional if the
parametersargument is not specified.A positive integer indicating which occurrence of
patterninsource_to return.string The search for the occurrence begins at position.If occurrenceis not present, defaults to1.If occurrenceis less than1or greater than the number of characters insource_, the search is ignored andstring REGEXP_returnsSUBSTR NULL. -
parameters: Optional.
When the parametersargument is specified, thepositionandoccurrencearguments must also be specified.It can have one of the following values: -
c: Perform case-sensitive matching. -
i: Perform case-insensitive matching.
If
coriis not specified, performs case-sensitive matching. -
Remarks
-
REGEXP_returnsSUBSTR NULLif no match is found,source_isstring NULL, orpatternisNULL.
Examples
The following examples demonstrate how REGEXP_ works when the global variable regexp_ is set to 'extended'.
|
Call to |
Result |
Comments |
|---|---|---|
|
|
|
Returns the substring |
|
|
|
Returns the character preceding the first occurrence of |
|
|
|
Returns the first digit. |
|
|
|
Returns the first alphabet. |
|
|
|
Looks for |
|
|
|
Returns the substring |
|
|
|
Searches for |
|
|
|
Returns the first occurrence of |
|
|
|
Returns the second occurrence of |
|
|
|
Returns the first occurrence of |
|
|
|
Returns the first occurrence of |
The following examples demonstrate how REGEXP_ works when the global variable regexp_ is set to 'advanced'.
|
Call to |
Result |
Comments |
|---|---|---|
|
|
|
Returns the first digit. |
|
|
|
Returns the first non-digit. |
|
|
|
Returns the first alpha-numeric character. |
|
|
|
Returns the first non-alpha-numeric character. |
|
|
|
Returns the first occurrence of the word |
Using REGEXP_ SUBSTR With a Table
The following example shows how to call REGEXP_ once per table row.
SET GLOBAL regexp_format = 'extended';DROP DATABASE IF EXISTS singlestore_docs_example;CREATE DATABASE singlestore_docs_example;use singlestore_docs_example;CREATE TABLE regexample_table(id INT, text_field TEXT);INSERT INTO regexample_table VALUES (1, 'the'), (2, 'athe'), (3, 'abthe'),(4, 'abcthe'), (5, 'thethe');SELECT id, text_field, REGEXP_SUBSTR(text_field,'..the') FROM regexample_tableORDER BY id;
+------+------------+-----------------------------------+
| id | text_field | REGEXP_SUBSTR(text_field,'..the') |
+------+------------+-----------------------------------+
| 1 | the | NULL |
| 2 | athe | NULL |
| 3 | abthe | abthe |
| 4 | abcthe | bcthe |
| 5 | thethe | hethe |
+------+------------+-----------------------------------+Last modified: September 25, 2025