Extract a substring by matching a regular expression.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String args[]) { Pattern pat = Pattern.compile("\\b\\w+@XYZ\\.com\\b"); Matcher mat = pat.matcher("t@XYZ.com\n" + "a@XYZ.com\n" + "n@XYZ.com"); while (mat.find()) System.out.println("Match: " + mat.group()); } } /* Match: t@XYZ.com Match: a@XYZ.com Match: n@XYZ.com */