regex_replace
Replaces occurrences of a regular expression pattern in a string with a replacement string. Unlike the simple replace function, this supports pattern matching with wildcards, character classes, quantifiers, and capture groups.
Syntax
regex_replace(source, pattern, replacement)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| source | string | Yes | The original string where replacements will be made. |
| pattern | string | Yes | The regular expression pattern to search for. |
| replacement | string | Yes | The replacement string. Can include backreferences such as \1, \2 for capture groups. |
Returns
Type: string
The modified string with all regex matches replaced.
Examples
Remove all digits from a string:
regex_replace([reference], "\d+", "")
If reference is "INC00012345", the result is "INC".
Collapse multiple spaces into one:
regex_replace([description], "\s+", " ")
If description is "Too many spaces", the result is "Too many spaces".
Rearrange name format using capture groups:
regex_replace([full_name], "(\w+) (\w+)", "\2, \1")
If full_name is "John Doe", the result is "Doe, John".
Notes
- All occurrences of the pattern are replaced. There is no option to limit the number of replacements.
- Use the
(?i)inline flag at the start of your pattern for case-insensitive matching, e.g.,"(?i)error". - Backreferences in the replacement string use
\1,\2, etc. to refer to captured groups. - If the pattern is invalid, the function raises an error. Test your regex patterns carefully.
Comments
0 comments
Please sign in to leave a comment.