Skip to content

Instantly share code, notes, and snippets.

@ppazos
Created October 4, 2017 17:57
Show Gist options
  • Save ppazos/303a20a7326fc10db1d9da08329b8275 to your computer and use it in GitHub Desktop.
Save ppazos/303a20a7326fc10db1d9da08329b8275 to your computer and use it in GitHub Desktop.
Gmail mail list name and email extraction
// Line Format
// email,
// name <email>,
// "name" <email>,
// Group 2 will have the name if available
// Group 3 will have the email
def raw = $/
[email protected],
[email protected],
"aaa bbb c." <[email protected]>,
aaaa áááá <[email protected]>,
[email protected],
aaa bbb <[email protected]>,
[email protected],
[email protected],
aaa bbb ccc ddd <[email protected]>,
aaa bbb <[email protected]>
/$
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "(\"?([a-zÀ-úÀ-ÿ\\.\\s]*)\"?\\s\\<)?((([a-z0-9-_]+\\.)*[a-z0-9-_]+)@(([a-z0-9-_]+\\.)*[a-z0-9-_]+)\\.[A-Z]{2,})(>?)(,?)*(\\s)*";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(raw);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment