Tableau function String

Tableau REGEXP_REPLACE to SQL

REGEXP_REPLACE(string, pattern, replacement) replaces every match of the pattern. DuckDB's regexp_replace() replaces only the first match unless you pass the 'g' option. Group references are written $1 in Tableau and \1 in DuckDB.

Updated · Checked on the xmetric engine

What is the syntax?

REGEXP_REPLACE(string, pattern, replacement)
string
the text.
pattern
a constant regular expression.
replacement
the text to insert; $1, $2 refer to capture groups.

Returns the string with every match replaced. Null if the string 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.

How do I mask every digit in a code?

Tableau

REGEXP_REPLACE([Code], '\d', '#')

SQL (DuckDB)

SELECT regexp_replace(Code, '\d', '#', 'g') AS result
FROM input
ORDER BY id

C-301-yz-9 becomes C-###-yz-#.

CodeResult
A-12-xA-##-x
B-71B-##
C-301-yz-9C-###-yz-#
a_1a_#
XX
D-4-yD-#-y
null

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

Where does a straight translation go wrong?

Common mistake

Why does DuckDB mask only the first digit?

Without 'g', regexp_replace stops after one match: A-12-x becomes A-#2-x. Tableau replaces them all.

Tableau

REGEXP_REPLACE([Code], '\d', '#')

Tempting SQL

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

SQL that matches

SELECT regexp_replace(Code, '\d', '#', 'g') AS result
FROM input
ORDER BY id
CodeTableauTempting SQLSQL that matches
A-12-xA-##-xA-#2-xA-##-x
B-71B-##B-#1B-##
C-301-yz-9C-###-yz-#C-#01-yz-9C-###-yz-#
a_1a_#a_#a_#
XXXX
D-4-yD-#-yD-#-yD-#-y
nullnullnull

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

Common mistake

Why does $2 come out literally in DuckDB?

DuckDB (RE2) writes group references as \1, \2; $2 is plain text there.

Tableau

REGEXP_REPLACE([Code], '(\w)-(\d+)', '$2/$1')

Tempting SQL

SELECT
  regexp_replace(Code, '(\w)-(\d+)', '$2/$1', 'g') AS result
FROM input
ORDER BY id

SQL that matches

SELECT
  regexp_replace(Code, '(\w)-(\d+)', '\2/\1', 'g') AS result
FROM input
ORDER BY id
CodeTableauTempting SQLSQL that matches
A-12-x12/A-x$2/$1-x12/A-x
B-7171/B$2/$171/B
C-301-yz-9301/C-y9/z$2/$1-y$2/$1301/C-y9/z
a_1a_1a_1a_1
XXXX
D-4-y4/D-y$2/$1-y4/D-y
nullnullnull

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

How do I reorder parts with capture groups?

Every match is rewritten, so both pairs in C-301-yz-9 move. Columns 1 to 7 are the sample rows, by id.

CaseTableau, then SQL (DuckDB)1234567
all matchesREGEXP_REPLACE([Code], '(\w)-(\d+)', '$2/$1')regexp_replace(Code, '(\w)-(\d+)', '\2/\1', 'g')12/A-x71/B301/C-y9/za_1X4/D-ynull

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_REPLACE(Code, '\\d', '#') AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

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

BigQuery documentation

What does your own REGEXP_REPLACE 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_REPLACE?

How do I refer to a capture group in the replacement?

Tableau uses $1: REGEXP_REPLACE([Code], '(\w)-(\d+)', '$2/$1') turns B-71 into 71/B. DuckDB writes the same as '\2/\1'.

Does REGEXP_REPLACE change nulls?

No; a null string stays null.

Where is this documented?