import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\b@[-\\w\\d.]*.[^gov\\t]+\\b";
final String string = "Love, Kenneth kenneth@teamtreehouse.com (555) 555-5555 Teacher, Treehouse @kennethlove\n"
+ "McFarland, Dave dave@teamtreehouse.com (555) 555-5554 Teacher, Treehouse\n"
+ "Arthur, King king_arthur@camelot.co.uk King, Camelot\n"
+ "Österberg, Sven-Erik governor@norrbotten.co.se Governor, Norrbotten @sverik\n"
+ ", Tim tim@killerrabbit.com Enchanter, Killer Rabbit Cave\n"
+ "Carson, Ryan ryan@teamtreehouse.com (555) 555-5543 CEO, Treehouse @ryancarson\n"
+ "Doctor, The doctor+companion@tardis.co.uk Time Lord, Gallifrey\n"
+ "Exampleson, Example me@example.com 555-555-5552 Example, Example Co. @example\n"
+ "Obama, Barack president.44@us.gov 555 555-5551 President, United States of America @potus44\n"
+ "Chalkley, Andrew andrew@teamtreehouse.com (555) 555-5553 Teacher, Treehouse @chalkers\n"
+ "Vader, Darth darth-vader@empire.gov (555) 555-4444 Sith Lord, Galactic Empire @darthvader\n"
+ "Fernández de la Vega Sanz, María Teresa mtfvs@spain.gov First Deputy Prime Minister, Spanish Govt.\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