Tableau function String

Tableau REGEXP_MATCH to SQL

REGEXP_MATCH(string, pattern) is true when any part of the string matches the pattern. DuckDB's regexp_matches() searches the same way. regexp_full_match() requires the pattern to cover the whole string, so it returns false far more often.

Updated · Checked on the xmetric engine

What is the syntax?

REGEXP_MATCH(string, pattern)
string
the text to test.
pattern
a constant regular expression.

Returns true if the pattern matches part of the string, false if not, null for a null string.

What does it return, and what is the SQL?

Each example runs on the same 7 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

Which codes contain a digit?

Tableau

REGEXP_MATCH([Code], '\d')

SQL (DuckDB)

SELECT regexp_matches(Code, '\d') AS result
FROM input
ORDER BY id

Every code but X.

CodeResult
A-12-xtrue
B-71true
C-301-yz-9true
a_1true
Xfalse
D-4-ytrue
null

The Tableau formula and the SQL return the same 7 rows.

Where does a straight translation go wrong?

Common mistake

Why does regexp_full_match reject A-12-x?

A full match must consume the whole string, and -x is left over. REGEXP_MATCH only needs the pattern to occur.

Tableau

REGEXP_MATCH([Code], '^[A-Z]-\d+')

Tempting SQL

SELECT regexp_full_match(Code, '^[A-Z]-\d+') AS result
FROM input
ORDER BY id

SQL that matches

SELECT regexp_matches(Code, '^[A-Z]-\d+') AS result
FROM input
ORDER BY id
CodeTableauTempting SQLSQL that matches
A-12-xtruefalsetrue
B-71truetruetrue
C-301-yz-9truefalsetrue
a_1falsefalsefalse
Xfalsefalsefalse
D-4-ytruefalsetrue
nullnullnull

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

Does an anchored start still match a substring?

The pattern must start the string but may stop before its end. Columns 1 to 7 are the sample rows, by id.

CaseTableau, then SQL (DuckDB)1234567
substringREGEXP_MATCH([Code], '^[A-Z]-\d+')regexp_matches(Code, '^[A-Z]-\d+')truetruetruefalsefalsetruenull

On every row, each Tableau formula and its SQL return the same value.

How do I write it in Snowflake or BigQuery?

The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.

Snowflake

SELECT REGEXP_INSTR(Code, '\\d') > 0 AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT REGEXP_CONTAINS(Code, r '\d') AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own REGEXP_MATCH formula return?

Edit the formula and run it on the 7 sample rows. Fields: [id], [Name], [Code], [Email], [Num].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about REGEXP_MATCH?

Is REGEXP_MATCH case sensitive?

Yes: REGEXP_MATCH([Email], 'SHOP') is false everywhere. Use a class such as [Ss][Hh][Oo][Pp], or lower the field first.

How do I require a match of the whole string?

Anchor the pattern: '^[A-Z]-\d+$'.

Where is this documented?