REGEXP_ SUBSTR
On this page
Searches a string for a regular expression pattern and returns the matching substring.
REGEXP_
supports the 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
occurrence
andparameters
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 position
is less than1
, the search begins at the first character ofsource_
.string If position
is greater than the number of characters insource_
, the result isstring NULL
. -
occurrence: Optional if
parameters
is not specified.A positive integer indicating which occurrence of
pattern
insource_
to return.string The search for the occurrence begins at position
.If occurrence
is not present, defaults to1
.If occurrence
is less than1
or greater than the number of characters insource_
, the search is ignored andstring REGEXP_
returnsSUBSTR NULL
. -
parameters:Optional.
If present, has one of the following values: -
c
: Perform case-sensitive matching. -
i
: Perform case-insensitive matching. -
If
c
ori
is not specified, performs case-sensitive matching.
-
Remarks
-
REGEXP_
returnsSUBSTR NULL
if no match is found,source_
isstring NULL
, orpattern
isNULL
.
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: August 9, 2023