3

In Gmail, all phone numbers get blue linked automatically. I used to manually take away the styles via HTML, but it has become tedious. I recently found out about how Regular Expressions can match the format of a string. So I am wondering if I can write a pattern that matches a phone number format. If it does match, I would like to apply a style to remove the blue link.

Not sure if this is how Regular Expressions even work, but I figured that I would give it a try.

So basically, I would like to find all phone numbers in the email body and apply styles to them if they match the pattern of the Regular Expression.

So far, I have this:

%%[
var @s, @o, @p

set @s = "uajakd0123456789dashdga"
set @p = "1?[\s-]?\(?(\d{3})\)?[\s-]?\d{3}[\s-]?\d{4}"
set @o = RegExMatch(@s, @p, 1)

outputLine(concat("input:  ",@s,"<br>"))
outputLine(concat('pattern: "',@p,'"<br>'))
outputLine(concat("output: ",@o,"<br>"))

]%%

I keep getting an output of only the first three numbers in a phone number of any format when this RegEx renders.

When I do this on RegExr.com, it works. But when I do it in Salesforce Marketing Cloud, it does not work. Does anybody have any idea why?

2

I'll be honest, I don't have a fancy explanation on this, but if you change the return in the RegExMatch function from 1 to 0, it returns the numbers as expected. Likely something to do with zero based numbering in SFMC AMPScript?

Like this: set @o = RegExMatch(@s, @p, 0)

So this:

%%[
var @s, @o, @p

set @s = "uajakd0123456789dashdga"
set @p = "1?[\s-]?\(?(\d{3})\)?[\s-]?\d{3}[\s-]?\d{4}"
set @o = RegExMatch(@s, @p, 1)

outputLine(concat("input:  ",@s,"<br>"))
outputLine(concat('pattern: "',@p,'"<br>'))
outputLine(concat("output: ",@o,"<br>"))

]%%

would become:

%%[
var @s, @o, @p

set @s = "uajakd0123456789dashdga"
set @p = "1?[\s-]?\(?(\d{3})\)?[\s-]?\d{3}[\s-]?\d{4}"
set @o = RegExMatch(@s, @p, 0)

outputLine(concat("input:  ",@s,"<br>"))
outputLine(concat('pattern: "',@p,'"<br>'))
outputLine(concat("output: ",@o,"<br>"))

]%%
  • Thanks! That worked! Now all I need to do is have it find these inside the email body and change them later. Thanks – Emailer Jan 13 '17 at 17:02
  • If you figure it out, I would be highly interested in hearing about it. That is a pretty interesting use case. – Gortonington Jan 13 '17 at 17:14
  • Yeah, I will share it. The reason I am doing this is because management was asking why all phone numbers turn blue in Gmail and wants it gone. I was just tired of manually writing inline styles in span tags every time we send out an email with a phone number. A solution that I thought of was writing some two variables. One with the opening tag with styles and one with a closing tag. This might be an alternative if I can't do it by RegEx – Emailer Jan 13 '17 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.