import java.io.*; import java.util.*; import java.util.regex.*; public class RegEx1 { public static void main(String[] args) throws Exception { // We have 3 possible values for our pattern. // This one matches a sequence of the characters 's', 't', 'e', or 'm'. String pat = "([stem]+)"; // The carat at the front of the square brackets negates the group. // Thus, this pattern matches any sequence of characters that is not 's', 't', 'e', or 'm'. //String pat = "([^stem]+)"; // The use of pipe symbols "|" provides a choice. The regular expression // can match "pat" or "match" or "File". //String pat = "(pat|match|File)"; Scanner s = new Scanner(new File(args[0])); while(s.hasNextLine()) { if(s.findInLine(pat) != null) { MatchResult m = s.match(); System.out.println("Match: "+m.group(1)); } s.nextLine(); } } }