Runtime verified Differs from official docs Test scripts included

Syntax

RegExMatch(sourceString, regExPattern, returnValue[, regExOptions, ...])  →  string

Parameters

Name Type Required Description
sourceString string Yes String to match against
regExPattern string Yes Regular expression to apply
returnValue string Yes Group selector as a quoted string; "0" returns the whole match, a group name returns that named group
regExOptions string No Option name such as IgnoreCase; no call with an option rendered in Next
regExOptionsN string No Further option names

At least three arguments. The forms that rendered pass the selector in quotes: a bare number as selector or as source breaks the render.

Example

%%=RegExMatch("order-4821-eu", "[0-9]+", "0")=%%

Renders 4821.

A named group works too. Build the < and > around the group name with Char(), because saving the email strips <num> out of a literal pattern as if it were a tag:

%%=RegExMatch("order-4821-eu", Concat("order-(?", Char(60), "num", Char(62), "[0-9]+)-"), "num")=%%

Renders 4821.

These are the only RegExMatch forms that rendered in a Next email. Test any other pattern or selector in a preview before you rely on it.

Return value

string — the text of the selected match.

The proven results are a whole-pattern match with selector "0" and a named group chosen by its name. What Next returns when nothing matches is not known, because every tested non-match broke the render (see below).

Behaviour

Quote the group selector. RegExMatch("order-4821-eu", "[0-9]+", "0") gives 4821. The same call with a bare 0, RegExMatch("order-4821-eu", "[0-9]+", 0), saves but makes the preview fail with HTTP 500.

Named groups work when the pattern is built with Char(). RegExMatch("order-4821-eu", Concat("order-(?", Char(60), "num", Char(62), "[0-9]+)-"), "num") gives 4821, as on Engagement. Typed as the literal "order-(?<num>[0-9]+)-", the pattern loses <num> when the email is saved, because the save strips anything that looks like a tag out of string literals.

Most tested calls break the render. The email saved and published, but the preview failed (HTTP 500, or no new rendered version) for each of these calls on its own:

Call Engagement renders
RegExMatch("SAVE23", "^[A-Z0-9]{5,7}$", 0) SAVE23
RegExMatch("order-4821-eu", "order-([0-9]+)-([a-z]+)", 2) eu
RegExMatch("order-4821-eu", "order-([0-9]+)-", 1) 4821
RegExMatch("order-4821-eu", "order-([0-9]+)-", "nope") (empty)
RegExMatch("order-4821-eu", "order-([0-9]+)-", 5) (empty)
RegExMatch("order-4821-eu", "[0-9]+", 1) (empty)
RegExMatch("order-4821-eu", "[0-9][0-9][0-9][0-9][0-9][0-9]", 0) (empty)
RegExMatch("a1 b2 c3", "[a-z][0-9]", 0) a1
RegExMatch("", "[0-9]+", 0) (empty)
RegExMatch(4821, "[0-9]+", 0) 4821
RegExMatch("ORDER-4821", "order", 0) (empty)
RegExMatch("ORDER-4821", "order", 0, "IgnoreCase") ORDER
RegExMatch("ORDER-4821", "order", 0, "ignorecase") ORDER
RegExMatch("ORDER-4821", "order.4821", 0, "IgnoreCase", "Singleline") ORDER-4821

Most of these pass a bare number somewhere, so they do not show whether capture groups, options or a non-match would work with a quoted selector. One call with a string selector failed as well: the missing group name "nope".

Bad input fails, as on Engagement. An unclosed group, RegExMatch("order-4821-eu", "order-([0-9]+", 0), breaks the render. An unknown option name, RegExMatch("order-4821-eu", "[0-9]+", 0, "NotARealOption"), and the two-argument call RegExMatch("order-4821-eu", "[0-9]+") are refused earlier: the content API answers HTTP 400 with “The AMPscript syntax isn’t valid” and the email cannot be saved.

Show Next test script
<!-- Marketing Cloud Next test script for RegExMatch. Paste it into the HTML component of a
     Next email and open the preview. Each line prints the result Next gave when the call
     was rendered on its own (expect) next to the result of this run (got).
     Assembled from single-call test emails (dates on the page); it has not been
     run as one email. Calls that make Next fail are listed separately. -->
<p>c1 expect=[4821] got=[%%=RegExMatch("order-4821-eu", "[0-9]+", "0")=%%]</p>
<p>c2 expect=[4821] got=[%%=RegExMatch("order-4821-eu", Concat("order-(?", Char(60), "num", Char(62), "[0-9]+)-"), "num")=%%]</p>

Show calls that fail in Next (one email each)
<!-- Run each call below in its own Next email: one of them in a combined email
     would stop the whole preview or block the save. -->

<!-- email 1: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("SAVE23","^[A-Z0-9]{5,7}$",0)=%%

<!-- email 2: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("order-4821-eu", "order-([0-9]+)-([a-z]+)", 2)=%%

<!-- email 3: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("ORDER-4821", "order", 0)=%%

<!-- email 4: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("ORDER-4821", "order", 0, "IgnoreCase")=%%

<!-- email 5: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("order-4821-eu", "[0-9]+", 0)=%%

<!-- email 6: expect that the email saves, but the preview never shows the new version (no fresh render) -->
%%=RegExMatch("order-4821-eu", "order-([0-9]+)-", 1)=%%

<!-- email 7: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("order-4821-eu", "order-([0-9]+)-", 5)=%%

<!-- email 8: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("order-4821-eu", "order-([0-9]+)-", "nope")=%%

<!-- email 9: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("order-4821-eu", "[0-9]+", 1)=%%

<!-- email 10: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("order-4821-eu", "[0-9][0-9][0-9][0-9][0-9][0-9]", 0)=%%

<!-- email 11: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("ORDER-4821", "order", 0, "ignorecase")=%%

<!-- email 12: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("ORDER-4821", "order.4821", 0, "IgnoreCase", "Singleline")=%%

<!-- email 13: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("ORDER-4821", "order.4821", 0, "IgnoreCase", "Singleline", "Multiline", "ExplicitCapture", "IgnorePatternWhitespace")=%%

<!-- email 14: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("a1 b2 c3", "[a-z][0-9]", 0)=%%

<!-- email 15: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("", "[0-9]+", 0)=%%

<!-- email 16: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch(4821, "[0-9]+", 0)=%%

<!-- email 17: expect that the save is rejected (HTTP 400) -->
%%=RegExMatch("order-4821-eu", "[0-9]+", 0, "NotARealOption")=%%

<!-- email 18: expect that the email saves, but the preview fails (HTTP 500) -->
%%=RegExMatch("order-4821-eu", "order-([0-9]+", 0)=%%

<!-- email 19: expect that the save is rejected (HTTP 400) -->
%%=RegExMatch("order-4821-eu", "[0-9]+")=%%

Differs from official docs

  • RegExMatch renders in Next, with a quoted selector. The reference lists it as not available in Marketing Cloud Next, yet RegExMatch("order-4821-eu", "[0-9]+", "0") rendered 4821, and a named group chosen by name rendered too. Card
  • A bare number as the group selector breaks the render. The reference passes 0 unquoted in its examples; in Next that form, and groups 1 and 2, made the preview fail. Only quoted selectors rendered ("0" and a group name). Card

Availability & differences

PlatformAvailable
Marketing Cloud EngagementYes
Marketing Cloud NextYes, from API 68.0

See also