---
schema_version: "1"
id: bdcadfd85252327038c4389b5d2c2e19b1da529ad4814ad61784ecb23937db30
title: RegExMatch()
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/marketing-cloud/ampscript/references/string/mc-ampscript-reference-string-regex-match.md
canonical_url: https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-string/mc-ampscript-reference-string-regex-match.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 4d0ddcd3349c2469fe4a233cd4f629e94e36953858d35672bb40aaa22080acba
products:
  - AMPscript for Marketing Cloud
  - Marketing Cloud
---
# 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

<table>
  <tr>
    <td>Marketing Cloud Engagement</td>
    <td>✅ Yes</td>
  </tr>
  <tr>
    <td>Marketing Cloud Next</td>
    <td>✅ Yes</td>
  </tr>
</table>

## Syntax

```
RegExMatch(sourceString,
           regExPattern,
           returnValue,
           repeatParameter)
```

The `RegExMatch()` function has four parameters:

- `sourceString` (string): Required. The string to evaluate.
- `regExPattern` (string): Required. The regular expression to apply to the source string.
- `returnValue` (string): Required. The name or ordinal of the matching group to return.
- `option` (string): An option to apply to the regular expression. See [Option Parameter](#option-parameter).

## Differences Between Marketing Cloud Engagement and Marketing Cloud Next

Both Marketing Cloud Next and Marketing Cloud Engagement support the `RegExMatch()` function, but they use different systems for evaluating regular expressions. If you use both products or plan to adapt content from one product to the other, review these differences.

### Patterns

Marketing Cloud Next doesn’t support these .NET-specific constructs:

- Character-class subtractions using the format `[a-z-[aeiou]]`.
- Inline comments using the format `(?#comment)`.
- Quote-delimited names in capturing groups, such as `(?'year'\d+)`. Instead, use angle brackets to specify a name for the capturing group.
- Duplicate names for capturing groups.
- Conditional matching using the format `(?(initialPattern)patternIfYes|patternIfNo)`.
- Balancing groups using the format `(?<name1-name2> pattern)`.
- The inline flag for enabling explicit capture (`(?n)`).

### Option Parameter

In Marketing Cloud Engagement, specify a regex option as a string. To apply more options, append them as parameters. The function supports all values from the [.NET RegexOptions enumeration](https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-options).

In Marketing Cloud Next, specify an option as a string. To apply more than one option, specify them as a list. For example, use `"IgnoreCase, Singleline"`. The function rejects a non-literal string in the `option` parameter, such as a variable associated with a list of strings.

Marketing Cloud Next supports only these options:

- `IgnoreCase`: Use case-insensitive matching.
- `Multiline`: Carat (`^`) and dollar sign (`$`) indicate the beginning and end of a line, respectively, instead of the beginning and end of the input string.
- `Singleline`: Dot (`.`) matches every character, including `\n`.
- `IgnorePatternWhitespace`: Exclude unescaped white space from the pattern.
- `ExplicitCapture`: A capturing group must have a name using the syntax `(?<name> pattern)`. The function ignores unnamed capturing groups.

Specifying other options in Marketing Cloud Next returns an error.

## 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.

```
%%[
  Var @couponCode, @regEx, @regExMatch
  Set @couponCode = "SAVE23"
  Set @regEx = "^[A-Z0-9]{5,7}$"
  Set @regExMatch = RegExMatch(@couponCode, @regEx, 0, "IgnoreCase")
  If Length(@regExMatch) > 0 then
]%%

<p>Your coupon code is %%=v(@regExMatch)=%%</p>
%%[ Else ]%%
<p>We weren't able to find a valid coupon code.</p>
%%[ EndIf ]%%

```

The example outputs a message indicating that the string matched the pattern in the regular expression.

```

Your coupon code is SAVE23

```

### Perform Complex Text Replacements

Combine the `RegExMatch()` function with the [Replace()](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-string/mc-ampscript-reference-string-replace.md) 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. The [ReplaceList()](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-string/mc-ampscript-reference-string-replace-list.md) function performs a similar function, but it works only with static strings. To handle irregularities in the source data, use regular expressions. This example works the same way if there are several spaces after the prefix or if the prefix doesn’t include a period.

```
<p>Customers:</p>
<ul>
%%[
  Var @namesRaw, @name, @rows, @row, @nameNormalized, @regexPattern
  Set @namesRaw = "Mr Tomás Santos, Ms. Jian Yeh, Miss Sun-Hi Kim, Dennis Smithers, Mrs Pooja Chatterjee, Mx. Dani Yellowknife, D'Angelo Cunningham"

  Set @rows = BuildRowSetFromString(@namesRaw, ",")
  Set @regexPattern = "(Mr\.?\s|Mrs\.?\s|Miss\s|Ms\.?\s|Mx\.?\s)"

  If RowCount(@rows) >= 1 then
    For @i = 1 to RowCount(@rows) do
      Set @row = Row(@rows, @i)
      Set @name = Field(@row, 1)
      Set @nameNormalized = Replace(@name, RegExMatch(@name, @regexPattern, 0), "")
]%%
  <li>%%=v(@nameNormalized)=%%</li>
%%[
    Next @i
  EndIf
]%%
</ul>
```

The code outputs a list of names with the prefixes removed.

```
Customers:

  • Tomás Santos
  • Jian Yeh
  • Sun-Hi Kim
  • Dennis Smithers
  • Pooja Chatterjee
  • Dani Yellowknife
  • D'Angelo Cunningham
```

## See Also

- _Microsoft Learn_: [.NET Regular Expressions](https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expressions)
- _RegularExpressions.info_: [Regular Expressions Tutorial](https://www.regular-expressions.info/tutorial.html)
