DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Convert Excel and CSV Documents to HTML in Java
  • Datafaker 2.0
  • How to Convert CSV to XML in Java
  • Getting Started With Agentic Workflows in Java and Quarkus

Trending

  • 8 Ways to Improve Application Performance
  • Introduction to Retrieval Augmented Generation (RAG)
  • Using the Spring @RequestMapping Annotation
  • S3 Vectors: How to Build a RAG Without a Vector Database
  1. DZone
  2. Coding
  3. Java
  4. Regular Expression to Validate a Comma-Separated List of Email Addresses

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!

By 
Greg Brown user avatar
Greg Brown
·
Aug. 24, 16 · Code Snippet
Likes (5)
Comment
Save
Tweet
Share
40.4K Views

Join the DZone community and get the full member experience.

Join For 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("[email protected]").matches()); // true
System.out.println(emailAddressPattern.matcher("[email protected], xyz").matches()); // false
System.out.println(emailAddressPattern.matcher("[email protected], [email protected]").matches()); // true


Java (programming language) CSV

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

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert Excel and CSV Documents to HTML in Java
  • Datafaker 2.0
  • How to Convert CSV to XML in Java
  • Getting Started With Agentic Workflows in Java and Quarkus

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook