Java: Get All Emails In String

Getting all emails is pretty easy to code if you know how to use regular expressions. Regular expressions ease and lessen your code to a minimum but at the same time giving out the same result as you would writing long code without using them. In Java, I use a regular expression to find all emails within a given string. The method returns a Vector object.

public static Vector findEmailsInContent(String content) {
  Vector emails = new Vector();
 
  Pattern pattern = Pattern.compile("(?:\\.\\.+)?([\\w\\-\\.]+@[\\w\\-\\.]+\\.\\w{2,3})");
  Matcher matcher = pattern.matcher(content);
 
  while (matcher.find()) {
    emails.addElement(matcher.group(1));
  }
 
  return emails;
}

Donations appreciated. Every little $ helps. Or click Google +1. Or play the short video on the upper right of this page.

Related Posts Plugin for WordPress, Blogger...

tags: , ,

Post a Comment