Last active
August 27, 2016 19:02
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// 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