REGEX_REPLACE_FIRST

All functions > STRING > REGEX_REPLACE_FIRST

Returns the string with the first occurrence of a regular expression pattern replaced.

Signatures

Returns: String with the first match replaced

REGEX_REPLACE_FIRST(string: VARCHAR, pattern: VARCHAR, replacement: VARCHAR) → VARCHAR
sql
ParameterTypeRequiredDescription
stringVARCHARYesString to search in
patternVARCHARYesRegular expression pattern to match
replacementVARCHARYesReplacement string (supports backreferences like \1)

Notes

  • Only the first match is replaced
  • Supports backreferences in the replacement string (\1, \2, etc.)
  • If the pattern does not match, returns the original string unchanged
  • Use REGEX_REPLACE_ALL to replace all occurrences

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
;
Result
f1 VARCHARf2 VARCHARf3 VARCHARf4 VARCHAR
abcNUMdef456baz bar foo01/15/2024no match

Last update at: 2026/03/03 16:47:38
Last updated: 2026-03-03 16:48:19