ReplaceRegEx - script and chart function
ReplaceRegEx() returns a string after replacing one or more matches between an input string and a specified regular expression pattern. The text that replaces the matching text is specified in the to_str argument. The function is non-recursive and works from left to right.
This function performs regex operations that are case-sensitive. You can alternatively use the variant ReplaceRegExI() to perform case-insensitive regex operations.
Syntax:
ReplaceRegEx (text, regex, to_str [, occurrence])
Return data type: string
Argument | Description |
---|---|
text | The input string text that you want to replace, fully or partially, with the new text from to_str. |
regex | The regular expression that defines when to replace text. Matches between this argument and the text argument are replaced. |
to_str | The new text that you want to use to replace existing content from text. |
occurrence |
The number of the match (between input text and regular expression) to replace with new text. This is an optional argument. The default is 0 if omitted. When a value of 0 is used, or if the argument is omitted, all matches between the text and regex are replaced with to_str. You can specify a negative value for occurrence if you want to identify matches from right to left. |
Example | Result |
---|---|
ReplaceRegEx('abc123','[0-9]','x') | Returns abcxxx. In this example, all text from the input string that matches the regex pattern are replaced with the new text. |
ReplaceRegEx('abc123','[0-9]','x',1) | Returns abcx23. Only the first occurrence of a match between the input string and the regex pattern are replaced. |
ReplaceRegEx('abc123','[0-9]','x',4) | Returns abc123 . There are only three possible matches between the input string and the regex pattern, so no modification is performed. |
ReplaceRegEx('ABC123','[a-c]','5') | Returns ABC123. No modification is performed because ReplaceRegEx() is case-sensitive, and the case-insensitive variant is not used. |
ReplaceRegExI('ABC123','[a-c]','5') | Returns 555123. The case-insensitive variant of the function, ReplaceRegExI(), is used. |
When to use it
You can use ReplaceRegEx() for modifying text to meet formatting and compliance standards, especially when complex patterns exist in the data. For example, you can re-format telephone number strings so that only numeric values are included.
If needed, you can also use this function to mask sensitive information, such as personal identifiable information (PII), so that it is not shown to users analyzing your app.