Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Regular Expression to Validate a Comma-Separated List of Email Addresses

DZone's Guide to

Regular Expression to Validate a Comma-Separated List of Email Addresses

Need to validate a CSV-formatted list of email addresses? Check out this regular expression and how to use it!

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

EDIT It has already been pointed out by several readers that this expression doesn't handle a number of common cases. See the comments or the original blog entry for an updated version.

Recently I needed to create a regular expression to validate the format of a comma-separated list of email addresses. Just thought I’d share the result in case it is of use to anyone:

\w+@\w+\.\w+(,\s*\w+@\w+\.\w+)*

Here’s an example of applying the pattern in Java:

// Compile pattern
Pattern emailAddressPattern = Pattern.compile(String.format("%1$s(,\\s*%1$s)*", "\\w+@\\w+\\.\\w+"));

// Validate addresses
System.out.println(emailAddressPattern.matcher("xyz").matches()); // false
System.out.println(emailAddressPattern.matcher("foo@bar.com").matches()); // true
System.out.println(emailAddressPattern.matcher("foo@bar.com, xyz").matches()); // false
System.out.println(emailAddressPattern.matcher("foo@bar.com, foo@bar.com").matches()); // true

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:
expression ,pattern ,regular expression ,email ,csv

Published at DZone with permission of Greg Brown, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}