Write a Regular Expression pattern that looks for phone numbers in the body of the email

Emailer 01/13/2017. 1 answers, 81 views
marketing-cloud ampscript regular-expressions

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?

1 Answers

Gortonington 01/13/2017.

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>"))

Emailer 01/13/2017
Thanks! That worked! Now all I need to do is have it find these inside the email body and change them later. Thanks
Gortonington 01/13/2017
If you figure it out, I would be highly interested in hearing about it. That is a pretty interesting use case.
Emailer 01/13/2017
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

HighResolutionMusic.com - Download Hi-Res Songs

1 Martin Garrix

Yottabyte flac

Martin Garrix. 2018. Writer: Martin Garrix.
2 Dyro

Latency flac

Dyro. 2018. Writer: Martin Garrix;Dyro.
3 Martin Garrix

Access flac

Martin Garrix. 2018. Writer: Martin Garrix.
4 Alan Walker

Diamond Heart flac

Alan Walker. 2018. Writer: Alan Walker;Sophia Somajo;Mood Melodies;James Njie;Thomas Troelsen;Kristoffer Haugan;Edvard Normann;Anders Froen;Gunnar Greve;Yann Bargain;Victor Verpillat;Fredrik Borch Olsen.
5 Sia

I'm Still Here flac

Sia. 2018. Writer: Sia.
6 Bradley Cooper

Shallow flac

Bradley Cooper. 2018. Writer: Andrew Wyatt;Anthony Rossomando;Mark Ronson;Lady Gaga.
7 Cardi B

Taki Taki flac

Cardi B. 2018. Writer: Bava;Juan Vasquez;Vicente Saavedra;Jordan Thorpe;DJ Snake;Ozuna;Cardi B;Selena Gomez.
8 Blinders

Breach (Walk Alone) flac

Blinders. 2018. Writer: Dewain Whitmore;Ilsey Juber;Blinders;Martin Garrix.
9 Halsey

Without Me flac

Halsey. 2018. Writer: Halsey;Delacey;Louis Bell;Amy Allen;Justin Timberlake;Timbaland;Scott Storch.
10 Lady Gaga

I'll Never Love Again flac

Lady Gaga. 2018. Writer: Benjamin Rice;Lady Gaga.
11 Mako

Rise flac

Mako. 2018. Writer: Riot Music Team;Mako;Justin Tranter.
12 Kelsea Ballerini

This Feeling flac

Kelsea Ballerini. 2018. Writer: Andrew Taggart;Alex Pall;Emily Warren.
13 Dewain Whitmore

Burn Out flac

Dewain Whitmore. 2018. Writer: Dewain Whitmore;Ilsey Juber;Emilio Behr;Martijn Garritsen.
14 Avril Lavigne

Head Above Water flac

Avril Lavigne. 2018. Writer: Stephan Moccio;Travis Clark;Avril Lavigne.
15 Lady Gaga

Look What I Found flac

Lady Gaga. 2018. Writer: DJ White Shadow;Nick Monson;Mark Nilan Jr;Lady Gaga.
16 Billie Eilish

When The Party's Over flac

Billie Eilish. 2018. Writer: Billie Eilish;FINNEAS.
17 Bradley Cooper

Always Remember Us This Way flac

Bradley Cooper. 2018. Writer: Lady Gaga;Dave Cobb.
18 Rita Ora

Let You Love Me flac

Rita Ora. 2018. Writer: Rita Ora.
19 Diplo

Electricity flac

Diplo. 2018. Writer: Diplo;Mark Ronson;Picard Brothers;Wynter Gordon;Romy Madley Croft;Florence Welch.
20 Charli XCX

1999 flac

Charli XCX. 2018. Writer: Charli XCX;Troye Sivan;Leland;Oscar Holter;Noonie Bao.

Related questions

Hot questions


Popular Tags