RegExMatch()

Searches for a pattern in a string using a regular expression.

A regular expression is a sequence of characters used for matching patterns in a string of text.

Availability 

Marketing Cloud Engagement ✅ Yes
Marketing Cloud Next ❌ No

Syntax 

1RegExMatch(sourceString,
2           regExPattern,
3           returnValue,
4           repeatParameter)

The RegExMatch() function has four parameters:

  • 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, @regExMatch
3  Set @couponCode = "SAVE23"
4  Set @regEx = "^[A-Z0-9]{5,7}$"
5  Set @regExMatch = RegExMatch(@couponCode, @regEx, 0, "IgnoreCase")
6  If Length(@regExMatch) > 0 then
7]%%
8
9<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, @regexPattern
5  Set @namesRaw = "Mr Tomás Santos, Ms. Jian Yeh, Miss Sun-Hi Kim, Dennis Smithers, Mrs Pooja Chatterjee, Mx. Dani Yellowknife, D'Angelo Cunningham"
6
7  Set @rows = BuildRowSetFromString(@namesRaw, ",")
8  Set @regexPattern = "(Mr\.?\s|Mrs\.?\s|Miss\s|Ms\.?\s|Mx\.?\s)"
9
10  If RowCount(@rows) >= 1 then
11    For @i = 1 to RowCount(@rows) do
12      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 @i
19  EndIf
20]%%
21</ul>

The code outputs a list of names with the prefixes removed.

1Customers:
2
3  • Tomás Santos
4  • Jian Yeh
5  • Sun-Hi Kim
6  • Dennis Smithers
7  • Pooja Chatterjee
8  • Dani Yellowknife
9  • D'Angelo Cunningham

See Also