RegEx2.java
Select all
/* RegEx2.java David Klick 3/30/05 Demonstrates regular expression use interactively. Requires 2cities.txt file as input. */ import java.util.regex.*; import java.io.*; public class RegEx2 { public static void main(String[] args) throws IOException { String line, pat; BufferedReader kbd = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter a pattern (
to quit): "); pat = kbd.readLine(); while (pat.length() > 0) { int matches = 0; BufferedReader in = new BufferedReader( new FileReader("2cities.txt")); line = in.readLine(); while (line != null) { if (line.matches(pat)) { if (matches++ == 0) System.out.println("Matches for " + pat + ": "); System.out.printf("%5d: %s\n", matches, line); } line = in.readLine(); } in.close(); if (matches == 0) System.out.println("No matches found!"); System.out.print("Enter a pattern (
to quit): "); pat = kbd.readLine(); } } }