Skip to content

Instantly share code, notes, and snippets.

@codingonHP
Last active August 27, 2016 19:02
Show Gist options
  • Save codingonHP/6649c55b0b81fe4abc731b8f7116a382 to your computer and use it in GitHub Desktop.
Save codingonHP/6649c55b0b81fe4abc731b8f7116a382 to your computer and use it in GitHub Desktop.
create a regular expression to validate the format of a comma-separated list of email addresses.
\w+@\w+\.\w+(,\s*\w+@\w+\.\w+)*
from: https://dzone.com/articles/regular-expression-to-validate-a-comma-separated-l?edition=206671&utm_source=Daily%20Digest&utm_medium=email&utm_campaign=dd%202016-08-27
@codingonHP
Copy link
Author

// 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment