All Products
Search
Document Center

MaxCompute:REGEXP_CONTAINS

Last Updated:Mar 26, 2026

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

ParameterRequiredTypeDescription
sourceYesSTRINGThe string to search.
patternYesSTRING constant or regular expressionThe pattern to match against source.

Return value

Returns a Boolean value.

ConditionReturn value
source contains a match for patterntrue
source does not contain a match for patternfalse
source or pattern is not a STRING or is invalidError
source or pattern is NULLNULL
pattern is an empty stringtrue

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.