StringRegexReplace

 

The StringRegexReplace function is a string substitution function using a regular expression.

 

string @StringRegexReplace(string source, string pattern, string replacement);

 

Parameters

string source : Target string

string pattern : Regular expression patterns

string replacement : Alternate strings

 

Return Values

Returns a substituted string according to the regular expression pattern.

 

Commonly Used Regular Expression Patterns

\d - Matches a single digit
\D - Matches a single non-digit character
\w - Matches a word character (alphabet, number, underscore)
\W - Matches a non-word character
\s - Matches a whitespace character
\S - Matches a non-whitespace character
[abc] - Matches one of a, b, or c
[^abc] - Matches any character except a, b, or c
+ - Repeats one or more times
* - Repeats zero or more times
? - Zero or one occurrence
{n} - Repeats exactly n times
{n,} - Repeats n or more times
{n,m} - Repeats between n and m times
^ - Start of string
$ - End of string
| - OR operator
() - Grouping

 


Example 1 (Remove HTML tags)

s = @StringRegexReplace("<p>Hello</p>", "<[^>]+>" , "");

 

Description: Finds HTML tag patterns and replaces them with an empty string. <[^>]+> finds all tags that start with '<' and end with '>'.

Result >>  s : Hello

 


Example 2 (Change phone number format)

s = @StringRegexReplace("010-1234-5678", "(\\d{3})-(\\d{4})-(\\d{4})","$1.$2.$3" );

 

Description: Changes hyphens (-) in a phone number to dots (.). Captures each group of digits and reconstructs them in a new format using $1, $2, $3 references.

                Result >>  s : 010.1234.5678

 


Example 3 (Remove all whitespace)

s = @StringRegexReplace("Hello nice to meet you", "\\s+", "");

 

Description: Finds one or more consecutive whitespace characters (\\s+) and removes them all. All spaces in the string disappear.

Result >> s : Hellonicetomaeetyou

 


Example 4 (Extract only numbers)

s = @StringRegexReplace("The price is 12,345 dollars", "[^\\d]", "");

 

Description: Replaces all non-digit characters ([^\\d]) with an empty string to leave only numbers. Commas, letters, and other characters are removed.

Result >> s : 12345

 


Example 5 (Change date format)

s = @StringRegexReplace("2025-03-18", "(\\d{4})-(\\d{2})-(\\d{2})", "$3/$2/$1");

 

Description: Changes date format from YYYY-MM-DD to DD/MM/YYYY. Captures each group of numbers and reconstructs them in a different order.

Result >> s : 18/03/2025

 


Example 6 (Replace specific words in a sentence)

s = @StringRegexReplace("Water level alarm notification is generating alarm sounds.", "alarm", "warning");

 

Description: Replaces all instances of the word "alarm" with "warning" in the sentence. All occurrences matching the pattern are changed.

Result >> s : Water level warning notification is generating warning sounds.

 


Example 7 (Change email domain)

s = @StringRegexReplace("user@old-domain.com", "@old-domain\\.com", "@new-domain.com" );

 

Description: Changes a specific domain in an email address to another domain. Finds the exact domain string and replaces it.

Result >> s : user@new-domain.com

 


 

Version Information

Support Version: 10.3.6.25 or later

 

Related Helps

@StringIndexOf()

@StringSubstring()

@StringTrim()

@StringTrimEnd()

@StringTrimStart()

@StringUnicodeToAnsi()