Java Printing Lines Containing a Pattern - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript Java Printing Lines Containing a Pattern - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript


Post Top Ad

Post Top Ad

Saturday, December 29, 2018

Java Printing Lines Containing a Pattern

Java Printing Lines Containing a Pattern


You need to look for lines matching a given regex in one or more files.


Write a simple grep-like program.


As I’ve mentioned, once you have a regex package, you can write a grep-like pro-
gram. 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, dif-
fering 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. * 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 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 4.12 presents a Grep2 program that uses my GetOpt (see Recipe 2.6) to parse command-line options.

* 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.
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(;
if (args.length != 1) {
System.err.println("Usage: Grep0 pattern");
Pattern patt = Pattern.compile(args[0]);
Matcher matcher = patt.matcher("");
String line = null;
while ((line = is.readLine( )) != null) {
if (matcher.find( )) {
System.out.println("MATCH: " + line);

No comments:

Post a Comment

Post Top Ad