REGEX_REPLACE_FIRST()

All functions > STRING > REGEX_REPLACE_FIRST()

Returns the haystack with the first regular-expression match replaced by the replacement string.

Signatures

Returns: Updated string; unchanged if there is no match; NULL if the haystack is NULL

REGEX_REPLACE_FIRST(string: VARCHAR, pattern: VARCHAR, replacement: VARCHAR) → VARCHAR
sql
ParameterTypeRequiredDescription
stringVARCHARYesString to search in
patternVARCHARYesRegular expression pattern to match
replacementVARCHARYesReplacement text; capture-group backreferences (a backslash followed by the group number) are supported where the underlying regex implementation allows

Notes

  • Only the first match is replaced; use REGEX_REPLACE_ALL for every match
  • If the pattern does not match, the original string is returned unchanged
  • Backreference syntax depends on the regex flavor; test critical replacements in your environment

See also

Examples

FeatureQL
SELECT
    f1 := REGEX_REPLACE_FIRST('abc123def456', '[0-9]+', 'NUM'), -- Replace first digit group
    f2 := REGEX_REPLACE_FIRST('foo bar foo', 'foo', 'baz'), -- Only first occurrence
    f3 := REGEX_REPLACE_FIRST('2024-01-15', '(\d{4})-(\d{2})-(\d{2})', '\2/\3/\1'), -- Backreference reorder
    f4 := REGEX_REPLACE_FIRST('no match', '[0-9]+', 'X'), -- No match returns original
    f5 := REGEX_REPLACE_FIRST(NULL(VARCHAR), 'a', 'b') -- NULL haystack
;
Result
f1 VARCHARf2 VARCHARf3 VARCHARf4 VARCHARf5 VARCHAR
abcNUMdef456baz bar foo01/15/2024no matchNULL

Last update at: 2026/05/26 17:22:09