Skip to content

Instantly share code, notes, and snippets.

@mguilherme
Created October 23, 2013 14:22
Show Gist options
  • Save mguilherme/7119758 to your computer and use it in GitHub Desktop.
Save mguilherme/7119758 to your computer and use it in GitHub Desktop.
Extract Mail Recipients (name and mail) from a given String
public Map<String, String> extractRecipients(String content) {
content = StringEscapeUtils.unescapeXml(content);
Map<String, String> recipients = new HashMap<>();
Pattern pattern = Pattern.compile("(.*?)<([^>]+)>\\s*;?,?", Pattern.DOTALL);
Matcher matcher = pattern.matcher(content);
while(matcher.find()) {
String name = matcher.group(1).replaceAll("[\\n\\r\"]+", "").trim();
String email = matcher.group(2).replaceAll("[\\n\\r\\s\"]+", "").trim();
recipients.put(name, email);
}
return recipients;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment