Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SFMC AMPScript RegEx
%%[
var @s, @o, @p, @m
output(concat("<br>Strip leading zeroes from a string"))
set @s = "0000012345"
set @p = "^0*(\d+)$"
set @o = RegExMatch(@s, @p, 1)
output(concat("<br>input: ", @s))
output(concat('<br>pattern: "', @p, '"'))
output(concat("<br>output: ", @o))
output(concat("<br><br>Check for all digits, no match"))
set @s = "12345x"
set @p = "^\d*$"
set @o = RegExMatch(@s, @p, 0)
output(concat("<br>input: ", @s))
output(concat('<br>pattern: "', @p, '"'))
output(concat("<br>output: ", @o))
output(concat("<br><br>Check for all digits, match"))
set @s = "12345"
set @p = "^\d*$"
set @o = RegExMatch(@s, @p, 0)
output(concat("<br>input: ", @s))
output(concat('<br>pattern: "', @p, '"'))
output(concat("<br>output: ", @o))
output(concat("<br><br>Replace parenthetical text with space"))
set @s = "whee (whatever it is) whoop"
set @p = "\s\(.+\)\s"
set @m = RegExMatch(@s, @p, 0, "IgnoreCase")
set @o = replace(@s, @m, " ")
output(concat("<br>input: ", @s))
output(concat('<br>pattern: "', @p, '"'))
output(concat("<br>match: ", @m))
output(concat("<br>replaced: ", @o))
]%%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.