alejorivera
alejorivera

Reputation: 945

Regex for multiple email addresses in one line

I currently have this regex:

/\w+@\w+\.\w+/

It works great for a string like this one:

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

However, it doesn't work for emails with two periods before or after the "@", for example:

[email protected]

How can I modify the regex for it to work with the second string as well?

Thanks! : )

Upvotes: 1

Views: 413

Answers (2)

Shafizadeh
Shafizadeh

Reputation: 10380

Use a character class instead of \w and add . into it. Something like this:

/^[a-zA-Z0-9_.]+@[a-zA-Z0-9_.]+$/

Online Demo

Upvotes: 1

DaveBensonPhillips
DaveBensonPhillips

Reputation: 3232

That's not surprising given you've hardcoded one period in your regex. Use \S+ to match any non-whitespace character:

>>> re.findall(r"\S+@\S+", "[email protected]")
['[email protected]']

Upvotes: 0

Related Questions