Here’s a simple example that gets you started. It allows you to search a pattern (-cpat) in a set of files that match a certain regular expression (-fpat). You can specify a directory from where it should start searching with the option -root. -R tells the grepper to recurse through subdirectories. Below are a couple examples.
Grep.java:
import java.util.regex.*;
import java.io.*;
public class Grep
{
Pattern pattern;
FileFilter fileFilter;
public Grep(String contentRegex) {
pattern = Pattern.compile(contentRegex);
}
public void searchInDir(String startDir, String dirRegex, boolean recurse)
throws IOException {
final Pattern patternFile = Pattern.compile(dirRegex);
fileFilter = new FileFilter() {
public boolean accept(File f) {
if (f.isDirectory()) return true;
Matcher m = patternFile.matcher(f.getName());
if (m.matches()) return true;
return false;
}
};
searchInDir_aux(new File(startDir), recurse);
}
public void searchInDir_aux(File dir, boolean recurse) throws IOException {
File[] contents = dir.listFiles(fileFilter);
for (int i=0; i<contents.length; i++) {
File fileToCheck = contents[i];
if (fileToCheck.isFile()) {
searchInFile(fileToCheck);
}
else if (recurse) {
searchInDir_aux(fileToCheck, recurse);
}
}
}
public void searchInFile(File file) throws IOException {
System.out.println("=====> " + file);
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
int count=0;
while ((line = br.readLine()) != null) {
count++;
Matcher m = pattern.matcher(line);
while (m.find()) {
System.out.println(count + ": " + line);
System.out.print(count + ": ");
indent(m.start());
System.out.print("^");
indent(m.end()-m.start()-2);
System.out.println("^");
}
}
br.close();
}
public static void indent(int h) {
for (int i=0; i<h; i++)
System.out.print(' ');
}
public static void main(String []args) throws Exception {
String contentPattern=null, filePattern=null, startDir=null;
boolean recurse = false;
int i = -1;
while (++i < args.length) {
String arg = args[i];
if (!arg.startsWith("-")) {
System.err.println("argument error: " + arg + "n");
printUsage();
System.exit(1);
}
if (arg.equals("-fpat")) {
if (i == args.length-1 || args[i+1].startsWith("-")) {
System.err.println("Missing filename RE pattern");
printUsage();
System.exit(1);
}
filePattern = args[++i];
continue;
}
else if (arg.equals("-cpat")) {
if (i == args.length-1 || args[i+1].startsWith("-")) {
System.err.println("Missing contents RE pattern");
printUsage();
System.exit(1);
}
contentPattern = args[++i];
continue;
}
else if (arg.equals("-R")) {
recurse = true;
continue;
}
else if (arg.equals("-root")) {
if (i == args.length-1 || args[i+1].startsWith("-")) {
System.err.println("Missing starting directory");
printUsage();
System.exit(1);
}
startDir = args[++i];
continue;
}
}
if (contentPattern == null || filePattern == null) {
printUsage();
System.exit(1);
}
if (startDir == null) {
startDir = "."; // take current directory
}
Grep grep = new Grep(contentPattern);
grep.searchInDir(startDir, filePattern, recurse);
}
private static void printUsage() {
System.err.println("Usage: java [optional] -fpat fileRegex -cpat contentRegex");
System.err.println("Where [optional] is one of the following:");
System.err.println(" -R recursively search subdirectories");
System.err.println(" -root <STARTDIR> specifies starting directory");
System.exit(1);
}
}
For example:
(searches all files for the pattern import)
C:myregextest> c:jdk1.4binjava Grep -fpat .* -cpat import
=====> .Grep$1.class
=====> .Grep.class
=====> .Grep.java
1: import java.util.regex.*;
1: ^ ^
2: import java.io.*;
2: ^ ^
(recursively searches c:winnt for files ending on .ini for the pattern micro.{5})
C:myregextest> c:jdk1.4binjava Grep -R -root c:winnt -fpat .*.ini -cpat micro.{5}
. . . [result]
A Grep utility that makes uses of Java’s RegEx package
Joris Van den BogaertHere’s a simple example that gets you started. It allows you to search a pattern (-cpat) in a set of files that match a certain regular expression (-fpat). You can specify a directory from where it should start searching with the option -root. -R tells the grepper to recurse through subdirectories. Below are a couple examples.
Grep.java:
For example: