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