Checks whether source contains a substring that matches the regular expression pattern. To match the entire string, anchor the pattern with ^ (start of line) and $ (end of line).
Syntax
BOOLEAN REGEXP_CONTAINS(STRING <source>, STRING <pattern>)Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
source | Yes | STRING | The string to search. |
pattern | Yes | STRING constant or regular expression | The pattern to match against source. |
Return value
Returns a Boolean value.
| Condition | Return value |
|---|---|
source contains a match for pattern | true |
source does not contain a match for pattern | false |
source or pattern is not a STRING or is invalid | Error |
source or pattern is NULL | NULL |
pattern is an empty string | true |
Examples
Validate an email address
SELECT
'foo@example.com' AS email,
REGEXP_CONTAINS('foo@example.com', '@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+') AS is_valid;Result:
+-----------------+----------+
| email | is_valid |
+-----------------+----------+
| foo@example.com | true |
+-----------------+----------+Match the entire string with ^ and $
Use ^ (start of line) and $ (end of line) to search for a full match. When using ^ and $ with alternation (|), wrap the alternatives in parentheses. Without parentheses, operator precedence applies only to the nearest term, not the entire expression — which can produce unexpected results.
The following example uses two inputs to show the difference.
Input: a@foo.com
SELECT
'a@foo.com' AS email,
REGEXP_CONTAINS('a@foo.com', '^([\\w.+-]+@foo\\.com|[\\w.+-]+@bar\\.org)$') AS valid_email_address,
REGEXP_CONTAINS('a@foo.com', '^[\\w.+-]+@foo\\.com|[\\w.+-]+@bar\\.org$') AS without_parentheses;Result:
+------------+---------------------+---------------------+
| email | valid_email_address | without_parentheses |
+------------+---------------------+---------------------+
| a@foo.com | true | true |
+------------+---------------------+---------------------+Both patterns return true for a@foo.com — the difference appears with the second input.
Input: !b@bar.org
SELECT
'!b@bar.org' AS email,
REGEXP_CONTAINS('!b@bar.org', '^([\\w.+-]+@foo\\.com|[\\w.+-]+@bar\\.org)$') AS valid_email_address,
REGEXP_CONTAINS('!b@bar.org', '^[\\w.+-]+@foo\\.com|[\\w.+-]+@bar\\.org$') AS without_parentheses;Result:
+------------+---------------------+---------------------+
| email | valid_email_address | without_parentheses |
+------------+---------------------+---------------------+
| !b@bar.org | false | true |
+------------+---------------------+---------------------+!b@bar.org starts with !, which is not a valid word character. With parentheses, ^ anchors the entire alternation and the result is correctly false. Without parentheses, ^ anchors only the first alternative ([\\w.+-]+@foo\\.com), so the second alternative ([\\w.+-]+@bar\\.org$) matches the suffix b@bar.org — returning true even though the full string is invalid.
Use an empty string as the pattern
-- Returns true.
WITH dummy AS(SELECT '' AS PATTERN) SELECT REGEXP_CONTAINS('ABC',PATTERN) FROM dummy;Related functions
REGEXP_CONTAINS is a string function. For more information about string search and transformation functions, see String functions.