Regex replace
Function: Regex replace
This function allows you to find specific patterns within a piece of text and replace them with new text. It's like a powerful "Find and Replace" tool that understands complex patterns, making it ideal for cleaning data, standardizing formats, or modifying text based on rules.
Input,
- Text: The original piece of text where you want to perform the replacement. This is a required input.
- Regex: The pattern you want to find within the text. This pattern uses a special language called "Regular Expressions" (Regex) to describe what to look for. For example,
\d+finds one or more digits, and[A-Z]+finds one or more uppercase letters. This is a required input. - Replacement: The new text that will replace every instance of the pattern found by your Regex. This is a required input.
Output,
- Result: The name of the variable where the modified text (after all replacements have been made) will be stored. By default, this will be named "RESULT".
Execution Flow,
Real-Life Examples,
Here are some examples of how you can use the "Regex replace" function:
Example 1: Removing unwanted characters from a price string
Imagine you have price data that sometimes includes currency symbols or extra spaces, and you want to clean it up to just the numbers.
- Inputs:
- Text: "$1,250.50 USD"
- Regex:
[^0-9\.](This pattern finds anything that is NOT a digit or a period.) - Replacement: "" (An empty string, effectively deleting the matched characters)
- Result: The variable
RESULTwill contain "1250.50".
Example 2: Standardizing phone number formats
You have a list of phone numbers in various formats, and you want to standardize them to \(XXX\) XXX-XXXX.
- Inputs:
- Text: "Call us at 123-456-7890 or (987) 654-3210."
- Regex:
\(\d\{3\}\)[ -.]*\(\d\{3\}\)[ -.]*\(\d\{4\}\)(This pattern captures three groups of digits separated by optional spaces, hyphens, or periods.) - Replacement:
\($1\) $2-$3(This uses the captured groups to reformat the number.)
- Result: The variable
RESULTwill contain "Call us at (123) 456-7890 or (987) 654-3210.".
Example 3: Anonymizing sensitive information in a log entry
You need to replace all email addresses in a log entry with a placeholder for privacy reasons.
- Inputs:
- Text: "User login from [email protected] at 2023-10-27. Another user [email protected] accessed the system."
- Regex:
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]\{2,\}\b(This pattern matches common email address formats.) - Replacement:
[EMAIL_ANONYMIZED]
- Result: The variable
RESULTwill contain "User login from [EMAIL_ANONYMIZED] at 2023-10-27. Another user [EMAIL_ANONYMIZED] accessed the system."