Tableau function String

Tableau REGEXP_EXTRACT to SQL

REGEXP_EXTRACT(string, pattern) returns the part of the string matched by the pattern's capturing group, or the whole match when there is no group. DuckDB's regexp_extract() does the same, with one difference: no match gives null in Tableau and an empty string in DuckDB.

Updated · Checked on the xmetric engine

What is the syntax?

REGEXP_EXTRACT(string, pattern)
string
the text to search.
pattern
a constant regular expression, usually with one capturing group.

Returns the matched capture group (or whole match) as a string. Null when nothing matches or the input is null.

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.

What is the first run of digits in each code?

Tableau

REGEXP_EXTRACT([Code], '(\d+)')

SQL (DuckDB)

SELECT nullif(regexp_extract(Code, '(\d+)', 1), '') AS result
FROM input
ORDER BY id

Code X has no digits, so the result is null.

CodeResult
A-12-x12
B-7171
C-301-yz-9301
a_11
Xnull
D-4-y4
null

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

Where does a straight translation go wrong?

Common mistake

Why does DuckDB return an empty string instead of null?

regexp_extract returns '' when nothing matches; Tableau returns null. Counts and ISNULL tests change. Wrap it in nullif(..., '').

Tableau

REGEXP_EXTRACT([Code], '(\d+)')

Tempting SQL

SELECT regexp_extract(Code, '(\d+)', 1) AS result
FROM input
ORDER BY id

SQL that matches

SELECT nullif(regexp_extract(Code, '(\d+)', 1), '') AS result
FROM input
ORDER BY id
CodeTableauTempting SQLSQL that matches
A-12-x121212
B-71717171
C-301-yz-9301301301
a_1111
Xnullnull
D-4-y444
nullnullnull

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

What if the pattern has no capturing group?

REGEXP_EXTRACT then returns the whole match. Columns 1 to 7 are the sample rows, by id.

CaseTableau, then SQL (DuckDB)1234567
no groupREGEXP_EXTRACT([Code], '\d+')nullif(regexp_extract(Code, '\d+'), '')12713011null4null

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_SUBSTR(Code, '(\\d+)', 1, 1, 'e', 1) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

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

BigQuery documentation

What does your own REGEXP_EXTRACT 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_EXTRACT?

Which regex flavour does Tableau use?

ICU on extracts; DuckDB uses RE2. Simple classes such as \d, \w and [a-z] behave the same; lookarounds and backreferences in patterns are not available in RE2.

How do I get the second group?

Use REGEXP_EXTRACT_NTH; in DuckDB, pass the group number as the third argument.

Where is this documented?