Regular Expressions in AMPScript

Did you know you can use regular expressions in AMPScript?

You can.

Here are some RegExMatch() snippets that I’ve used:

Variables

@s = input string
@p = pattern
@m = match
@o = output

Sample Code

Output

Strip leading zeroes from a string
input: 0000012345
pattern: “^0*(\d+)$”
output: 12345

Check for all digits, no match
input: 12345x
pattern: “^\d*$”
output:

Check for all digits, match
input: 12345
pattern: “^\d*$”
output: 12345

Replace parenthetical text with space
input: whee (whatever it is) whoop
pattern: “\s(.+)\s”
match: (whatever it is)
replaced: whee whoop

For further reading

RegExMatch() function
Concat() function
OutputLine() function
RegEx pattern reference
RegEx options reference

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Blake Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Blake
Guest
Blake

thanks