Java: Get All Emails In String
Posted by admin on
February 13, 2009
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; }









