Antoder
Antoder

Reputation: 65

Regex to match multiple emails separated by space or puncts

I need a regex to match multiple email address separated by space(s) and/or puncts. The email addresses are into a string. I'm trying this, but it doesn't works

^(\s[[:punct:]])*([a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}){1}(\s[[:punct:]])*$

Text could be like this:

[email protected], [email protected] [email protected]

Or even like this

Lorem ipsum dolor sit [email protected] amet, consectetur, [email protected], adipiscing elit. Vestibulum consectetur fringilla mi ac dignissim. Nulla at est quam. [email protected] Sed enim.

Upvotes: 0

Views: 582

Answers (1)

David Chan
David Chan

Reputation: 7505

you could replace the ^ and $ anchors with something that doesn't force the email to be both first and last in the line...

like \b (word boundry)

\b(\s[[:punct:]])*([a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}){1}(\s[[:punct:]])*\b

or nothing at all ?

(\s[[:punct:]])*([a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}){1}(\s[[:punct:]])*

http://rubular.com/r/AMnr2kzp09

or simply

(\b[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}\b)

since the {1} was restricting the number of emails you could match between ^ and $

Upvotes: 1

Related Questions