-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJGrep.java
36 lines (28 loc) · 936 Bytes
/
JGrep.java
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
33
34
35
36
import java.util.regex.*;
import java.io.*;
public class JGrep{
public static void main(String[] args)throws Exception{
if(args.length!=1){
System.err.println("Greps text from input according to pattern, and outputs the matched patterns and groups in tab-separated format.");
System.err.println("Usage: <regex pattern> < input > output ");
return;
}
//System.err.println("REGEX: "+args[0]);
Pattern pat=Pattern.compile(args[0]);
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in,"utf8"));
PrintWriter writer=new PrintWriter(new OutputStreamWriter(System.out,"utf8"),true);
for(String line;(line=reader.readLine())!=null;){
Matcher m=pat.matcher(line);
while(m.find()){
writer.print(m.group(0));
for(int i=1;i<=m.groupCount();i++){
writer.print('\t');
writer.print(m.group(i));
}
writer.println();
}
}
reader.close();
writer.close();
}
}