How to get email address from a string
I am searching for a way to retrieve all emails addresses from a given string. For example: if i have the string "AB CD [ABCD@gmail.com]" i want to recei开发者_如何学Pythonve only "ABCD@gmail.com".
I guess i should use RegExp and String match function, but i am not sure how.
Any ideas?
String.match
is indeed the way to go:
var email:String = "foo foo bar@barbar.com 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.
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.
精彩评论