1 57 58 import java.io.*; 59 import org.apache.oro.text.regex.*; 60 import org.apache.oro.text.awk.*; 61 62 69 public final class streamInputExample { 70 71 79 public static final void main(String args[]) { 80 81 String regex = "(\\w[^\\.?!]*C\\+\\+|C\\+\\+)[^\\.?!]*[\\.?!]"; 85 String sentence; 86 AwkMatcher matcher; 87 AwkCompiler compiler; 88 Pattern pattern = null, newline = null; 89 AwkStreamInput input; 90 MatchResult result; 91 Reader file = null; 92 93 compiler = new AwkCompiler(); 95 matcher = new AwkMatcher(); 96 97 try { 100 pattern 101 = compiler.compile(regex, AwkCompiler.CASE_INSENSITIVE_MASK); 102 newline = compiler.compile("(\\s*[\n\r]\\s*)+"); 105 } catch(MalformedPatternException e) { 106 System.err.println("Bad pattern."); 107 System.err.println(e.getMessage()); 108 System.exit(1); 109 } 110 111 112 try { 114 file = new FileReader("streamInputExample.txt"); 115 } catch(IOException e) { 116 System.err.println("Error opening streamInputExample.txt."); 117 System.err.println(e.getMessage()); 118 System.exit(1); 119 } 120 121 input = new AwkStreamInput(file); 123 124 try { 128 while(matcher.contains(input, pattern)) { 130 result = matcher.getMatch(); 132 133 sentence = Util.substitute(matcher, newline, 135 new StringSubstitution(" "), 136 result.toString(), Util.SUBSTITUTE_ALL); 137 System.out.println("\nMatch:\n" + sentence); 138 } 139 } catch(IOException e) { 140 System.err.println("Error occurred while reading file."); 141 System.err.println(e.getMessage()); 142 System.exit(1); 143 } 144 } 145 } 146 | Popular Tags |