-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrep.java
More file actions
33 lines (28 loc) · 1.02 KB
/
Copy pathGrep.java
File metadata and controls
33 lines (28 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.io.*;
import java.util.regex.*;
/**
This class demonstrates how to read a line of text from the keyboard
Based on http://www.abbeyworkshop.com/howto/java/readLine/ReadLine.java
*/
public class Grep{
public static void main(String[] args){
try {
Pattern pattern = Pattern.compile(args[0]);
String line = "";
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
line = in.readLine();
while (line != null){
Matcher matcher = pattern.matcher(line);
if(matcher.find()) {
System.out.println(line);
}
line = in.readLine();
}
}catch (IOException e){
System.out.println("IO Exception\n");
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("Usage: java Grep REGEX (and then type strings on STDIN, Ctr-D to exit)\n");
}
}
}