import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\\w+)(?:(?:@)|(?:\\s+[Aa][Tt]\\s+))(.+)(?:(?:\\.edu)|(?:\\s+[dD][oO][Tt]\\s+edu))";
final String string = "ashishg e ashishg@stanford.edu\n"
+ "ashishg e rozm@stanford.edu\n"
+ "ashishg p 650-723-1614\n"
+ "ashishg p 650-723-4173\n"
+ "ashishg p 650-814-1478\n"
+ "balaji e balaji@stanford.edu\n"
+ "bgirod p 650-723-4539\n"
+ "bgirod p 650-724-3648\n"
+ "bgirod p 650-724-6354\n"
+ "cheriton e cheriton@cs.stanford.edu\n"
+ "cheriton e uma@cs.stanford.edu\n"
+ "cheriton p 650-723-1131\n"
+ "cheriton p 650-725-3726\n"
+ "dabo e dabo@cs.stanford.edu\n"
+ "dabo p 650-725-3897\n"
+ "dabo p 650-725-4671\n"
+ "dlwh e dlwh@stanford.edu\n"
+ "engler e engler@lcs.mit.edu\n"
+ "engler e engler@stanford.edu\n"
+ "eroberts e eroberts@cs.stanford.edu\n"
+ "eroberts p 650-723-3642\n"
+ "eroberts p 650-723-6092\n"
+ "fedkiw e fedkiw@cs.stanford.edu\n"
+ "hager e hager@cs.jhu.edu\n"
+ "hager p 410-516-5521\n"
+ "hager p 410-516-5553\n"
+ "hager p 410-516-8000\n"
+ "hanrahan e hanrahan@cs.stanford.edu\n"
+ "hanrahan p 650-723-0033\n"
+ "hanrahan p 650-723-8530\n"
+ "horowitz p 650-725-3707\n"
+ "horowitz p 650-725-6949\n"
+ "jks e jks@robotics.stanford.edu\n"
+ "jurafsky e jurafsky@stanford.edu\n"
+ "jurafsky p 650-723-5666\n"
+ "kosecka e kosecka@cs.gmu.edu\n"
+ "kosecka p 703-993-1710\n"
+ "kosecka p 703-993-1876\n"
+ "kunle e darlene@csl.stanford.edu\n"
+ "kunle e kunle@ogun.stanford.edu\n"
+ "kunle p 650-723-1430\n"
+ "kunle p 650-725-3713\n"
+ "kunle p 650-725-6949\n"
+ "lam e lam@cs.stanford.edu\n"
+ "lam p 650-725-3714\n"
+ "lam p 650-725-6949\n"
+ "latombe e asandra@cs.stanford.edu\n"
+ "latombe e latombe@cs.stanford.edu\n"
+ "latombe e liliana@cs.stanford.edu\n"
+ "latombe p 650-721-6625\n"
+ "latombe p 650-723-0350\n"
+ "latombe p 650-723-4137\n"
+ "latombe p 650-725-1449\n"
+ "levoy e ada@graphics.stanford.edu\n"
+ "levoy e melissa@graphics.stanford.edu\n"
+ "levoy p 650-723-0033\n"
+ "levoy p 650-724-6865\n"
+ "levoy p 650-725-3724\n"
+ "levoy p 650-725-4089\n"
+ "manning e dbarros@cs.stanford.edu\n"
+ "manning e manning@cs.stanford.edu\n"
+ "manning p 650-723-7683\n"
+ "manning p 650-725-1449\n"
+ "manning p 650-725-3358\n"
+ "nass e nass@stanford.edu\n"
+ "nass p 650-723-5499\n"
+ "nass p 650-725-2472\n"
+ "nick e nick.parlante@cs.stanford.edu\n"
+ "nick p 650-725-4727\n"
+ "ok p 650-723-9753\n"
+ "ok p 650-725-1449\n"
+ "ouster e teresa.lynn@stanford.edu\n"
+ "ouster e ouster@cs.stanford.edu\n"
+ "pal e pal@cs.stanford.edu\n"
+ "pal p 650-725-9046\n"
+ "psyoung e patrick.young@stanford.edu\n"
+ "rajeev p 650-723-4377\n"
+ "rajeev p 650-723-6045\n"
+ "rajeev p 650-725-4671\n"
+ "rinard e rinard@lcs.mit.edu\n"
+ "rinard p 617-253-1221\n"
+ "rinard p 617-258-6922\n"
+ "serafim e serafim@cs.stanford.edu\n"
+ "serafim p 650-723-3334\n"
+ "serafim p 650-725-1449\n"
+ "shoham e shoham@stanford.edu\n"
+ "shoham p 650-723-3432\n"
+ "shoham p 650-725-1449\n"
+ "subh e subh@stanford.edu\n"
+ "subh e uma@cs.stanford.edu\n"
+ "subh p 650-724-1915\n"
+ "subh p 650-725-3726\n"
+ "subh p 650-725-6949\n"
+ "thm e pkrokel@stanford.edu\n"
+ "thm p 650-725-3383\n"
+ "thm p 650-725-3636\n"
+ "thm p 650-725-3938\n"
+ "tim p 650-724-9147\n"
+ "tim p 650-725-2340\n"
+ "tim p 650-725-4671\n"
+ "ullman e support@gradiance.com\n"
+ "ullman e ullman@cs.stanford.edu\n"
+ "ullman p 650-494-8016\n"
+ "ullman p 650-725-2588\n"
+ "ullman p 650-725-4802\n"
+ "vladlen e vladlen@stanford.edu\n"
+ "widom e siroker@cs.stanford.edu\n"
+ "widom e widom@cs.stanford.edu\n"
+ "widom p 650-723-0872\n"
+ "widom p 650-723-7690\n"
+ "widom p 650-725-2588\n"
+ "zelenski e zelenski@cs.stanford.edu\n"
+ "zelenski p 650-723-6092\n"
+ "zelenski p 650-725-8596\n"
+ "zm e manna@cs.stanford.edu\n"
+ "zm p 650-723-4364\n"
+ "zm p 650-725-4671\n"
+ "650-725-4671\n"
+ "650-725-4671\n"
+ "6103564533\n"
+ "(610)3564533\n"
+ "(610) 356-4533 \n"
+ "610.405.0420\n"
+ "6104050420\n"
+ "16104050420\n"
+ "610.405-0420\n"
+ "610.405-0420\n\n\n\n\n";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
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));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html