Regular expression functions - Amazon Timestream

Regular expression functions

The regular expression functions in Timestream for LiveAnalytics support the Java pattern syntax. Timestream for LiveAnalytics supports the following regular expression functions.

Function Output data type Description

regexp_extract_all(string, pattern)

array(varchar)

Returns the substring(s) matched by the regular expression pattern in string.

SELECT regexp_extract_all('example expect complex', 'ex\w')

Example result: [ exa,exp ]

regexp_extract_all(string, pattern, group)

array(varchar)

Finds all occurrences of the regular expression pattern in string and returns the capturing group number group.

SELECT regexp_extract_all('example expect complex', '(ex)(\w)', 2)

Example result: [ a,p ]

regexp_extract(string, pattern)

varchar

Returns the first substring matched by the regular expression pattern in string.

SELECT regexp_extract('example expect', 'ex\w')

Example result: exa

regexp_extract(string, pattern, group)

varchar

Finds the first occurrence of the regular expression pattern in string and returns the capturing group number group.

SELECT regexp_extract('example expect', '(ex)(\w)', 2)

Example result: a

regexp_like(string, pattern)

boolean

Evaluates the regular expression pattern and determines if it is contained within string. This function is similar to the LIKE operator, except that the pattern only needs to be contained within string, rather than needing to match all of string. In other words, this performs a contains operation rather than a match operation. You can match the entire string by anchoring the pattern using ^ and $.

SELECT regexp_like('example', 'ex')

Example result: true

regexp_replace(string, pattern)

varchar

Removes every instance of the substring matched by the regular expression pattern from string.

SELECT regexp_replace('example expect', 'expect')

Example result: example

regexp_replace(string, pattern, replacement)

varchar

Replaces every instance of the substring matched by the regex pattern in string with replacement. Capturing groups can be referenced in replacement using $g for a numbered group or ${name} for a named group. A dollar sign ($) may be included in the replacement by escaping it with a backslash (\$).

SELECT regexp_replace('example expect', 'expect', 'surprise')

Example result: example surprise

regexp_replace(string, pattern, function)

varchar

Replaces every instance of the substring matched by the regular expression pattern in string using function. The lambda expression function is invoked for each match with the capturing groups passed as an array. Capturing group numbers start at one; there is no group for the entire match (if you need this, surround the entire expression with parenthesis).

SELECT regexp_replace('example', '(\w)', x -> upper(x[1]))

Example result: EXAMPLE

regexp_split(string, pattern)

array(varchar)

Splits string using the regular expression pattern and returns an array. Trailing empty strings are preserved.

SELECT regexp_split('example', 'x')

Example result: [ e,ample ]