sourceString (string): Required. The string to search using regular expression.
regExPattern (string): Required. The regular expression to use in the search.
returnValue (string): Required. The name or ordinal of the matching group to return.
repeatParameter (string): The repeating string parameter to apply. You can use any value from the .NET RegexOptions enumeration, such as IgnoreCase and Multiline.
Usage
To use the function, pass it a string, a regular expression to apply to the string, and the value from the regular expression that you want to return.
Perform a Simple Search Using a Regular Expression
This example uses a regular expression to determine if it contains 5–7 alphanumeric characters.
1%%[2 Var @couponCode, @regEx, @regExMatch3 Set @couponCode = "SAVE23"4 Set @regEx = "^[A-Z0-9]{5,7}$"5 Set @regExMatch = RegExMatch(@couponCode, @regEx, 0, "IgnoreCase")6 If Length(@regExMatch) > 0 then7]%%89<p>Your coupon code is %%=v(@regExMatch)=%%</p>10%%[ Else ]%%11<p>We weren't able to find a valid coupon code.</p>12%%[ EndIf ]%%
The example outputs a message indicating that the string matched the pattern in the regular expression.
1Your coupon code is 1943832
Perform Complex Text Replacements
You can combine the RegExMatch() function with the Replace() function to replace text in a string based on a regex pattern. This example uses the RegExMatch() function to find name prefixes such as Mr. and Mrs. It then uses the Replace() function to remove those prefixes from each name in a rowset. You could perform a similar operation by using the ReplaceList() function. However, the ReplaceList() function can only replace static strings. By using regular expressions, you can handle irregularities in the source data. For example, the regular expression used in this example works the same if there are multiple spaces after the prefix, or if the prefix isn’t followed by a period.
1<p>Customers:</p>2<ul>3%%[4 Var @namesRaw, @name, @rows, @row, @nameNormalized, @regexPattern5 Set @namesRaw = "Mr Tomás Santos, Ms. Jian Yeh, Miss Sun-Hi Kim, Dennis Smithers, Mrs Pooja Chatterjee, Mx. Dani Yellowknife, D'Angelo Cunningham"67 Set @rows = BuildRowSetFromString(@namesRaw, ",")8 Set @regexPattern = "(Mr\.?\s|Mrs\.?\s|Miss\s|Ms\.?\s|Mx\.?\s)"910 If RowCount(@rows) >= 1 then11 For @i = 1 to RowCount(@rows) do12 Set @row = Row(@rows, @i)13 Set @name = Field(@row, 1)14 Set @nameNormalized = Replace(@name, RegExMatch(@name, @regexPattern, 0), "")15]%%16 <li>%%=v(@nameNormalized)=%%</li>17%%[18 Next @i19 EndIf20]%%21</ul>
The code outputs a list of names with the prefixes removed.