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)
SELECTnullif(regexp_extract(Code, '(\d+)', 1), '') AS result
FROM input
ORDERBY id
Code X has no digits, so the result is null.
Code
Result
A-12-x
12
B-71
71
C-301-yz-9
301
a_1
1
X
null
D-4-y
4
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
SELECTregexp_extract(Code, '(\d+)', 1) AS result
FROM input
ORDERBY id
SQL that matches
SELECTnullif(regexp_extract(Code, '(\d+)', 1), '') AS result
FROM input
ORDERBY id
Code
Tableau
Tempting SQL
SQL that matches
A-12-x
12
12
12
B-71
71
71
71
C-301-yz-9
301
301
301
a_1
1
1
1
X
null
null
D-4-y
4
4
4
null
null
null
✓ 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.
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.