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
| Parameter | Type | Required | Description |
|---|---|---|---|
string | VARCHAR | Yes | String to search in |
pattern | VARCHAR | Yes | Regular expression pattern to match |
replacement | VARCHAR | Yes | Replacement 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_ALLfor 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 VARCHAR | f2 VARCHAR | f3 VARCHAR | f4 VARCHAR | f5 VARCHAR |
|---|---|---|---|---|
| abcNUMdef456 | baz bar foo | 01/15/2024 | no match | NULL |
On this page