regexp_replace()

If string contains the regex, replaces the matching part with replacement. A set of optional regex options can be set.

Reference: Haybarn WASM 2.0.0-rc12

Arguments & returns

2 overloads

regexp_replace(string, regex, replacement)

Scalar
Overload link stringregexreplacement Returns
#01 VARCHARVARCHARVARCHAR VARCHAR

regexp_replace(string, regex, replacement, options)

Scalar
Overload link stringregexreplacementoptions Returns
#02 VARCHARVARCHARVARCHARVARCHAR VARCHAR

Replace text with regular expressions

Clean strings by replacing text that matches a regular expression. Use the g option when every match should be replaced.

A NULL input produces NULL. A string without a match is returned unchanged.

Replace text with regular expressions
SQL
Haybarn WASM 2.0.0-rc12 · In your browser

Guide example · Run to view results.

Expected result
first_matchall_matchesmissing
A# B34A# B#NULL

Watch for: Without the g option, only the first match is replaced. Use replace() when the search text is literal rather than a pattern.

Example

regexp_replace example
SQL
Haybarn WASM 2.0.0-rc12 · In your browser

Engine example · Run to view results.

In a query

Turn a label into a useful key.

Trim the edges, normalize case, and replace runs of spaces. Small functions compose into a readable cleaning step.

Turn a label into a useful key.
SQL
Haybarn WASM 2.0.0-rc12 · In your browser

Guide recipe · Run to view results.

In other engines

PostgreSQLregexp_replace()

Same function

Exasolregexp_replace()

REGEXP_REPLACE(subject, pattern, replacement, position, occurrence)
In Haybarn
REGEXP_REPLACE(subject, pattern, replacement)

SQLGlot example ↗

Snowflakeregexp_replace()

REGEXP_REPLACE(subject, pattern)
In Haybarn
REGEXP_REPLACE(subject, pattern, '', 'g')

SQLGlot example ↗