Regular Expression Functions¶
Regular expression functions use RE2 as the regex engine. RE2 is fast, but supports only a subset of PCRE syntax and in particular does not support backtracking and associated features (e.g. back references). See https://github.com/google/re2/wiki/Syntax for more information.
- like(string, pattern) boolean¶
- like(string, pattern, escape) boolean
Evaluates if the
stringmatches thepattern. Patterns can contain regular characters as well as wildcards. Wildcard characters can be escaped using the single character specified for theescapeparameter. Matching is case sensitive.Note: The wildcard ‘%’ represents 0, 1 or multiple characters and the wildcard ‘_’ represents exactly one character.
SELECT like(‘abc’, ‘%b%’); – true SELECT like(‘a_c’, ‘%#_%’, ‘#’); – true
- regexp_extract(string, pattern) varchar¶
Returns the first substring matched by the regular expression
patterninstring:SELECT regexp_extract('1a 2b 14m', '\d+'); -- 1
- regexp_extract(string, pattern, group) varchar¶
Finds the first occurrence of the regular expression
patterninstringand returns the capturing group numbergroup:SELECT regexp_extract('1a 2b 14m', '(\d+)([a-z]+)', 2); -- 'a'
- regexp_extract_all(string, pattern, group)¶
Finds all occurrences of the regular expression
patterninstringand returns the capturing group numbergroup:SELECT regexp_extract_all('1a 2b 14m', '(\d+)([a-z]+)', 2); -- ['a', 'b', 'm']
- regexp_like(string, pattern) boolean¶
Evaluates the regular expression
patternand determines if it is contained withinstring.This function is similar to the
LIKEoperator, except that the pattern only needs to be contained withinstring, rather than needing to match all ofstring. 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('1a 2b 14m', '\d+b'); -- true
- regexp_replace(string, pattern) varchar¶
Removes every instance of the substring matched by the regular expression
patternfromstring:SELECT regexp_replace('1a 2b 14m', '\d+[ab] '); -- '14m'
- regexp_replace(string, pattern, replacement) varchar¶
Replaces every instance of the substring matched by the regular expression
patterninstringwithreplacement. Capturing groups can be referenced inreplacementusing$gfor 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('1a 2b 14m', '(\d+)([ab]) ', '3c$2 '); -- '3ca 3cb 14m'