Regular Expression SUBSTRING
In the regular expression form (with SIMILAR
), the SUBSTRING
function returns part of the string matching an SQL regular expression pattern.If no match is found, NULL
is returned.
The SIMILAR
pattern is formed from three SQL regular expression patterns, R1, R2 and R3.The entire pattern takes the form of R1 || '<escape>"' || R2 || '<escape>"' || R3
, where <escape> is the escape character defined in the ESCAPE
clause.R2 is the pattern that matches the substring to extract, and is enclosed between escaped double quotes (<escape>"
, e.g. “#"
” with escape character ‘#
’).R1 matches the prefix of the string, and R3 the suffix of the string.Both R1 and R3 are optional (they can be empty), but the pattern must match the entire string.In other words, it is not sufficient to specify a pattern that only finds the substring to extract.
Tip
|
The escaped double quotes around R2 can be compared to defining a single capture group in more common regular expression syntax like PCRE.That is, the full pattern is equivalent to |
Note
|
If any one of R1, R2, or R3 is not a zero-length string and does not have the format of an SQL regular expression, then an exception is raised. |
The full SQL regular expression format is described in Syntax: SQL Regular Expressions
substring('abcabc' similar 'a#"bcab#"c' escape '#') -- bcab
substring('abcabc' similar 'a#"%#"c' escape '#') -- bcab
substring('abcabc' similar '_#"%#"_' escape '#') -- bcab
substring('abcabc' similar '#"(abc)*#"' escape '#') -- abcabc
substring('abcabc' similar '#"abc#"' escape '#') -- <null>