REGEXP_LIKE
Returns true if the string matches the regular expression. This function is similar to the LIKE-predicate, except that it uses regular expressions rather than simple wildcard character matching.
Syntax
REGEXP_LIKE( string, pattern[, modifiers ] )
Parameters
string
|
The |
||||||||||||
pattern
|
A string containing the regular expression to match against the string. The syntax of the regular expression is compatible with the Perl 5 regular expression syntax. See the Perl Regular Expressions Documentation for details. |
||||||||||||
modifiers
|
[Optional] One or more single-character flags that modify how the regular expression finds matches in string:
|
Notes
This function operates on UTF-8 strings using the default locale, even if the locale has been set to something else.
If you are porting a regular expression query from an Oracle database, remember that Oracle considers a zero-length string to be equivalent to NULL, while Vertica does not.
Examples
This example creates a table containing several strings to demonstrate regular expressions.
=> CREATE TABLE t (v VARCHAR); CREATE TABLE => CREATE PROJECTION t1 AS SELECT * FROM t; CREATE PROJECTION => COPY t FROM stdin; Enter data to be copied followed by a newline. End with a backslash and a period on a line by itself. >> aaa >> Aaa >> abc >> abc1 >> 123 >> \. => SELECT * FROM t; v ------- aaa Aaa abc abc1 123 (5 rows)
Select all records in the table that contain the letter "a."
=> SELECT v FROM t WHERE REGEXP_LIKE(v,'a'); v ------ Aaa aaa abc abc1 (4 rows)
Select all of the rows in the table that start with the letter "a."
=> SELECT v FROM t WHERE REGEXP_LIKE(v,'^a'); v ------ aaa abc abc1 (3 rows)
Select all rows that contain the substring "aa."
=> SELECT v FROM t WHERE REGEXP_LIKE(v,'aa'); v ----- Aaa aaa (2 rows)
Select all rows that contain a digit.
=> SELECT v FROM t WHERE REGEXP_LIKE(v,'\d'); v ------ 123 abc1 (2 rows)
Select all rows that contain the substring "aaa."
=> SELECT v FROM t WHERE REGEXP_LIKE(v,'aaa'); v ----- aaa (1 row)
Select all rows that contain the substring "aaa" using case insensitive matching.
=> SELECT v FROM t WHERE REGEXP_LIKE(v,'aaa', 'i'); v ----- Aaa aaa (2 rows)
Select rows that contain the substring "a b c."
=> SELECT v FROM t WHERE REGEXP_LIKE(v,'a b c'); v --- (0 rows)
Select rows that contain the substring "a b c" ignoring space within the regular expression.
=> SELECT v FROM t WHERE REGEXP_LIKE(v,'a b c','x'); v ------ abc abc1 (2 rows)
Add multi-line rows to demonstrate using the "m" modifier.
=> COPY t FROM stdin RECORD TERMINATOR '!'; Enter data to be copied followed by a newline. End with a backslash and a period on a line by itself. >> Record 1 line 1 >> Record 1 line 2 >> Record 1 line 3! >> Record 2 line 1 >> Record 2 line 2 >> Record 2 line 3! >> \.
Select rows that start with the substring "Record" and end with the substring "line 2."
=> SELECT v from t WHERE REGEXP_LIKE(v,'^Record.*line 2$'); v --- (0 rows)
Select rows that start with the substring "Record" and end with the substring "line 2," treating multiple lines as separate strings.
=> SELECT v from t WHERE REGEXP_LIKE(v,'^Record.*line 2$','m'); v -------------------------------------------------- Record 2 line 1 Record 2 line 2 Record 2 line 3 Record 1 line 1 Record 1 line 2 Record 1 line 3 (2 rows)