/*** * streamInputExample.java,v 1.4 1997/08/14 05:25:38 dfs Exp * * This is a test program demonstrating how to search an InputStream * with the OROMatcher Perl5 regular expression classes. It also * demonstrates for those not familiar with regular expressions, how * to use parenthesized groups and backreferences. * * Copyright 1997 ORO, Inc. All rights reserved. ***/ import java.io.*; import com.oroinc.text.regex.*; public final class streamInputExample { /*** * This program tries to find SSN values whose birth and death dates * have the same month and day. See the file streamInputExample.txt * for more details. This program uses the streamInputExample.txt file * as sample input. ***/ public static final void main(String args[]) { // A regular expression that will find data entries with identical // birth and death dates (month and day). Notice how the backreferences // \2 and \3 take care of this. The first parenthesized group is used // to preserve the SSN value, while the second and third are used to // record the day and month of birth for use by the backreferences. // The fourth and fifth groups save the birth and death years. String regex = "SSN:\\s+(\\d+)\\s+Month:\\s+(\\d+)\\s+Day:\\s+(\\d+)\\s+Year:\\s+(\\d+)" + "\\s+Month:\\s+\\2\\s+Day:\\s+\\3\\s+Year:\\s+(\\d+)"; Perl5Matcher matcher; Perl5Compiler compiler; Perl5Pattern pattern = null; Perl5StreamInput input; MatchResult result; InputStream file = null; // Create Perl5Compiler and Perl5Matcher instances. compiler = new Perl5Compiler(); matcher = new Perl5Matcher(); // Attempt to compile the pattern. If the pattern is not valid, // report the error and exit. try { pattern = (Perl5Pattern)compiler.compile(regex, Perl5Compiler.CASE_INSENSITIVE_MASK); } catch(MalformedPatternException e) { System.err.println("Bad pattern."); System.err.println(e.getMessage()); System.exit(1); } // Open input file. try { file = new FileInputStream("streamInputExample.txt"); } catch(IOException e) { System.err.println("Error opening streamInputExample.txt."); System.err.println(e.getMessage()); System.exit(1); } // Create a Perl5StreamInput instance to search the input stream. input = new Perl5StreamInput(file); // We need to put the search loop in a try block because when searching // a Perl5StreamInput instance, an IOException may occur, and it must be // caught. try { // Loop until there are no more matches left. while(matcher.contains(input, pattern)) { // Since we're still in the loop, fetch match that was found. result = matcher.getMatch(); // Print the SSN which was saved in group 1. System.out.println("SSN: " + result.group(1)); // Print the month which was saved in group 2. System.out.println("Month: " + result.group(2)); // Print the day which was saved in group 3. System.out.println("Day: " + result.group(3)); // Print the birth year which was saved in group 4. System.out.println("Birth Year: " + result.group(4)); // Print the death year which was saved in group 5. System.out.println("Death Year: " + result.group(5) + "\n"); } } catch(IOException e) { System.err.println("Error occurred while reading file."); System.err.println(e.getMessage()); System.exit(1); } } }