views:

30

answers:

2

Hi

I am searching for a way to retrieve all emails addresses from a given string. For example: if i have the string "AB CD [[email protected]]" i want to receive only "[email protected]".

I guess i should use RegExp and String match function, but i am not sure how.

Any ideas?

A: 

I suggest you to read here about a proper regexp to match emails. And yes, you should use the match function of your language but keep in mind that you have to adapt your regex to your email pattern, like, in this case, you may include the presence of [ and ].

I also advice to use Regex Buddy to try and test regexp, it's the best tool out there imho.

dierre
+4  A: 

String.match is indeed the way to go:

var email:String = "foo foo [email protected] sploof";
var matches:Array = email.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g); 

This uses a regex to match a RFC2822 from the community regexes of regexr.

grapefrukt
great answer. Thanks :)
Erik Sapir