Regular expression functions
Use these functions to test, extract, replace, and split text with regular expressions, when a pattern needs more than the wildcards of LIKE.
Patterns use RE2 syntax, which does not support lookahead or lookbehind assertions. The ~ (bitwise NOT and regular expression match) operator also tests whether a regular expression matches part of a string.
regexp_escape function
Returns string with a backslash added before every ASCII character that is not a letter, a digit, or an underscore. Characters outside ASCII, such as the euro sign (chr(8364)), are not changed. Use the result as a pattern that matches string literally, for example when you build a pattern from text that contains characters such as ., +, or ?.
Syntax
regexp_escape(string)
Arguments
The regexp_escape function takes the following argument:
string-
The string to escape, as a
VARCHARvalue.
Return type
VARCHAR
Examples
The following SQL query escapes a price label that contains a dollar sign, a period, a space, and parentheses. The result has a backslash before each of these characters.
SELECT regexp_escape('$5.00 (approx)') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares an escaped pattern with an unescaped one. Escaped, 1+1 matches only the literal text 1+1, so it does not match 11. Unescaped, + means one or more, so the pattern matches.
SELECT regexp_matches( '11', regexp_escape('1+1') ) AS escaped, regexp_matches('11', '1+1') AS unescaped
The query returns the following result.
| escaped | unescaped |
|---|---|
|
|
regexp_extract function
Returns the first part of string that matches the regular expression regex. By default, returns the whole match. With group, returns that capturing group instead.
With name_list, returns every capturing group at once, as a STRUCT with one field for each name in name_list, in order.
If regex does not match, returns an empty string (or a STRUCT whose fields are empty strings), not NULL.
Syntax
regexp_extract(string, regex [, group ] [, options ])
regexp_extract(string, regex, name_list [, options ])
Arguments
The regexp_extract function takes the following arguments:
string-
The string to search, as a
VARCHARvalue. regex-
The regular expression, as a
VARCHARvalue. group-
The capturing group to return. It must be a constant
INTEGERvalue. The default is0, which returns the whole match. This argument is optional. name_list-
The field names, one for each capturing group in
regex. It must be a constant list ofVARCHARvalues (VARCHAR[]). options-
The option letters, such as
'i'for case-insensitive matching. It must be a constantVARCHARvalue. This argument is optional.
Return type
VARCHAR. With name_list, a STRUCT with one VARCHAR field for each name, for example STRUCT(y VARCHAR, m VARCHAR, d VARCHAR).
Examples
The following SQL query returns the first capturing group, with case-insensitive matching.
SELECT regexp_extract( 'ABC', '([a-z])(b)', 1, 'i' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query has no match, so it returns an empty string, not NULL.
SELECT regexp_extract('abc', 'x') = '' AS is_empty
The query returns the following result.
| is_empty |
|---|
|
The following SQL query returns the parts of a date as a STRUCT with named fields.
SELECT regexp_extract( '2023-04-15', '(\d+)-(\d+)-(\d+)', ['y', 'm', 'd'] ) AS result
The query returns the following result.
| result |
|---|
|
Differences from Athena engine version 3
In Athena engine version 3, regexp_extract returns NULL when there is no match. In DuckDB, it returns an empty string. To get the Athena engine version 3 result, use NULLIF(regexp_extract(.string, regex), '')
regexp_extract_all function
Returns a list with one element for each match of the regular expression regex in string. Matches do not overlap, so a character is never part of two matches. By default, each element is the whole match. With group, each element is that capturing group.
With name_list, each element is a STRUCT that holds the capturing groups of one match, with one field for each name in name_list.
If there is no match, returns an empty list.
Syntax
regexp_extract_all(string, regex [, group
[, options ] ])
regexp_extract_all(string, regex, name_list
[, options ])
Arguments
The regexp_extract_all function takes the following arguments:
string-
The string to search, as a
VARCHARvalue. regex-
The regular expression, as a
VARCHARvalue. group-
The capturing group to return from each match, as an
INTEGERvalue. The default is0, which returns the whole match. This argument is optional. name_list-
The field names, one for each capturing group in
regex. It must be a constant list ofVARCHARvalues (VARCHAR[]). options-
The option letters, such as
'i'for case-insensitive matching. It must be a constantVARCHARvalue. This argument is optional.
Return type
VARCHAR[]. With name_list, a list of STRUCT values, for example STRUCT(person VARCHAR, age VARCHAR)[].
Examples
The following SQL query returns the second capturing group, the age, from each match.
SELECT regexp_extract_all( 'Peter: 33, Paul:14', '(\w+):\s*(\d+)', 2 ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query omits group, so each element of the list is a whole match.
SELECT regexp_extract_all( 'Peter: 33, Paul:14', '(\w+):\s*(\d+)' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the capturing groups of each match as a STRUCT with named fields.
SELECT regexp_extract_all( 'Peter: 33, Paul:14', '(\w+):\s*(\d+)', ['person', 'age'] ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query has no match, so it returns an empty list, and the list has a length of 0.
SELECT len( regexp_extract_all('abc', '[0-9]') ) AS match_count
The query returns the following result.
| match_count |
|---|
|
Considerations
To pass options without name_list, you must also pass group. Use 0 to return whole matches.
regexp_full_match function
Returns true if the regular expression regex matches all of string, from the first character to the last, and false otherwise. To test for a match anywhere in the string, use the regexp_matches function.
Syntax
regexp_full_match(string, regex [, options ])
Arguments
The regexp_full_match function takes the following arguments:
string-
The string to test, as a
VARCHARvalue. regex-
The regular expression, as a
VARCHARvalue. options-
The option letters, such as
'i'for case-insensitive matching. It must be a constantVARCHARvalue. This argument is optional.
Return type
BOOLEAN
Examples
The following SQL query returns false, because (an)* matches only part of the string.
SELECT regexp_full_match('anabanana', '(an)*') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares regexp_full_match with regexp_matches for a pattern that matches only the middle of the string.
SELECT regexp_full_match('abc', 'b') AS whole_string, regexp_matches('abc', 'b') AS anywhere
The query returns the following result.
| whole_string | anywhere |
|---|---|
|
|
The following SQL query uses the 'i' option for case-insensitive matching.
SELECT regexp_full_match('ABC', 'abc', 'i') AS result
The query returns the following result.
| result |
|---|
|
regexp_matches function
Returns true if any part of string matches the regular expression regex, and false otherwise. To require the match to start at the beginning or finish at the end of the string, anchor the pattern with ^ or $, or use the regexp_full_match function.
A pattern that can match an empty string, such as (an)*, matches every string.
Syntax
regexp_matches(string, regex [, options ])
Arguments
The regexp_matches function takes the following arguments:
string-
The string to search, as a
VARCHARvalue. regex-
The regular expression, as a
VARCHARvalue. options-
The option letters, such as
'i'for case-insensitive matching. It must be a constantVARCHARvalue. This argument is optional.
Return type
BOOLEAN
Examples
The following SQL query returns true, because (an)* can match an empty string, so it matches every string.
SELECT regexp_matches('anabanana', '(an)*') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares an unanchored pattern with a pattern anchored to the start of the string.
SELECT regexp_matches('abc', 'b') AS contains_b, regexp_matches('abc', '^b') AS starts_with_b
The query returns the following result.
| contains_b | starts_with_b |
|---|---|
|
|
The following SQL query compares the default case-sensitive match with the 'i' option.
SELECT regexp_matches('ABC', 'b') AS case_sensitive, regexp_matches('ABC', 'b', 'i') AS case_insensitive
The query returns the following result.
| case_sensitive | case_insensitive |
|---|---|
|
|
Considerations
Regular expressions use RE2 syntax, which does not support lookahead or lookbehind assertions such as (?=...). A pattern that uses them fails. Rewrite the pattern without the assertion.
Differences from Athena engine version 3
Athena engine version 3 tests for a match with regexp_like. DuckDB does not have regexp_like. Use regexp_matches, which also returns true for a match anywhere in the string.
In Athena engine version 3, a regular expression can use lookahead and lookbehind assertions, such as (?=...). In DuckDB, regular expressions use RE2 syntax, which does not support them, so a query whose pattern uses them fails. Rewrite the pattern without the assertion.
regexp_replace function
Returns string with the first match of the regular expression regex replaced by replacement. To replace every match, pass the 'g' option. If there is no match, returns string unchanged.
In replacement, \1 through \9 insert the text of the matching capturing group, and \0 inserts the whole match.
Syntax
regexp_replace(string, regex, replacement [, options ])
Arguments
The regexp_replace function takes the following arguments:
string-
The string to search, as a
VARCHARvalue. regex-
The regular expression, as a
VARCHARvalue. replacement-
The text that replaces each match, as a
VARCHARvalue. It can contain group references such as\1. options-
The option letters, such as
'g'to replace every match and'i'for case-insensitive matching. It must be a constantVARCHARvalue. You can combine letters, as in'gi'. This argument is optional.
Return type
VARCHAR
Examples
The following SQL query replaces only the first match, because it does not pass the 'g' option.
SELECT regexp_replace('hello', '[lo]', '-') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes the 'g' option to replace every match.
SELECT regexp_replace('hello', '[lo]', '-', 'g') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses group references to reorder the parts of a date.
SELECT regexp_replace( '2021-03-04', '(\d+)-(\d+)-(\d+)', '\3/\2/\1' ) AS result
The query returns the following result.
| result |
|---|
|
Differences from Athena engine version 3
In Athena engine version 3, regexp_replace replaces every match. In DuckDB, it replaces only the first match unless you pass the 'g' option. To get the Athena engine version 3 result, use regexp_replace(.string, regex, replacement, 'g')
In Athena engine version 3, you write a group reference in replacement as $1. In DuckDB, write \1 instead, because DuckDB inserts $1 as literal text.
In Athena engine version 3, you can omit replacement to remove every match. DuckDB requires replacement. To remove every match, pass '' and the 'g' option.
regexp_split_to_table function
Splits string at each match of the regular expression regex and returns each part as a separate row. It is equivalent to unnest(string_split_regex(.string, regex))
To get the parts as a list in one row, use the string_split_regex function.
Syntax
regexp_split_to_table(string, regex)
Arguments
The regexp_split_to_table function takes the following arguments:
string-
The string to split, as a
VARCHARvalue. regex-
The regular expression that matches the separators, as a
VARCHARvalue.
Return type
VARCHAR, one row for each part.
Examples
The following SQL query splits a string at each digit.
SELECT regexp_split_to_table('a1b2c', '[0-9]') AS part ORDER BY part
The query returns the following results.
| part |
|---|
|
|
|
The following SQL query returns one row for each tag in a comma-separated column. Each row keeps the id of the row that it came from.
SELECT id, regexp_split_to_table(tags, ',') AS tag FROM (VALUES (1, 'red,blue'), (2, 'green') ) AS t(id, tags) ORDER BY id, tag
The query returns the following results.
| id | tag |
|---|---|
|
|
|
|
|
|
Considerations
Call regexp_split_to_table in the select list. It is not a table function, so you cannot call it in the FROM clause.
If string is NULL, regexp_split_to_table returns no rows, so the row that it came from does not appear in the result.
string_split_regex function
Splits string at each match of the regular expression regex and returns the parts as a list. If a match is at the start or end of string, the list starts or ends with an empty element.
To return one row for each part, use the regexp_split_to_table function.
regexp_split_to_array and str_split_regex are aliases of string_split_regex.
Syntax
string_split_regex(string, regex [, options ])
Arguments
The string_split_regex function takes the following arguments:
string-
The string to split, as a
VARCHARvalue. regex-
The regular expression that matches the separators, as a
VARCHARvalue. options-
The option letters, such as
'i'for case-insensitive matching. It must be a constantVARCHARvalue. This argument is optional.
Return type
VARCHAR[]
Examples
The following SQL query splits a string at each space that has an optional semicolon before it.
SELECT string_split_regex( 'hello world; 42', ';? ' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the alias regexp_split_to_array to split a string at each run of digits.
SELECT regexp_split_to_array('a1b22c', '[0-9]+') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query counts the parts that each function returns for the separator . in the string a.b. As a regular expression, . matches any character, so string_split_regex splits at every character and returns four empty parts. The string_split function treats . as literal text, so it splits only at the period and returns two parts.
SELECT len( string_split_regex('a.b', '.') ) AS regex_part_count, len(string_split('a.b', '.')) AS literal_part_count
The query returns the following result.
| regex_part_count | literal_part_count |
|---|---|
|
|
Differences from Athena engine version 3
Athena engine version 3 splits a string at a regular expression with regexp_split. DuckDB does not have regexp_split. Use string_split_regex or its alias regexp_split_to_array.