Java Cookbook, Second Edition
Problem
You need to look for lines matching a given regex in one or more files. Solution
Write a simple grep-like program. Discussion
As I've mentioned, once you have a regex package, you can write a grep-like program. I gave an example of the Unix grep program earlier. grep is called with some optional arguments, followed by one required regular expression pattern, followed by an arbitrary number of filenames. It prints any line that contains the pattern, differing from Recipe 4.5, which prints only the matching text itself. For example: grep "[dD]arwin" *.txt searches for lines containing either darwin or Darwin in every line of every file whose name ends in .txt.[3] Example 4-5 is the source for the first version of a program to do this, called Grep0. It reads lines from the standard input and doesn't take any optional arguments, but it handles the full set of regular expressions that the Pattern class implements (it is, therefore, not identical with the Unix programs of the same name). We haven't covered the java.io package for input and output yet (see Chapter 10), but our use of it here is simple enough that you can probably intuit it. The online source includes Grep1, which does the same thing but is better structured (and therefore longer). Later in this chapter, Recipe Recipe 4.12 presents a Grep2 program that uses my GetOpt (see Recipe 2.6) to parse command-line options. [3] On Unix, the shell or command-line interpreter expands *.txt to match all the filenames, but the normal Java interpreter does this for you on systems where the shell isn't energetic or bright enough to do it. Example 4-5. Grep0.java
import java.io.*; import java.util.regex.*; /** Grep0 - Match lines from stdin against the pattern on the command line. */ public class Grep0 { public static void main(String[] args) throws IOException { BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); if (args.length != 1) { System.err.println("Usage: Grep0 pattern"); System.exit(1); } Pattern patt = Pattern.compile(args[0]); Matcher matcher = patt.matcher(""); String line = null; while ((line = is.readLine( )) != null) { matcher.reset(line); if (matcher.find( )) { System.out.println("MATCH: " + line); } } } } |