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

Aliases

  • REGEXP_REPLACE_FIRST

See also

Examples

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

Last update at: 2026/06/20 10:08:10