-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
59 lines (52 loc) · 1.77 KB
/
Main.java
File metadata and controls
59 lines (52 loc) · 1.77 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package home;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Pattern;
/*
D:\!data\Java\Grep\out\production\Grep>C:\Downloads\Chrome\GetGnuWin32\gnuwin32\bin\cat test.txt | java home.Main
*/
public class Main {
public static void main(String[] args) {
// System.out.println("Hello world!");
boolean meth = true;
ArrayList<String> keyWords = new ArrayList<String>();
ArrayList<Pattern> regs = new ArrayList<Pattern>();
if (args.length > 0) {
Pattern p;
for (String i : args) {
if (meth) {
p = Pattern.compile(i);
regs.add(p);
System.out.println("Added regex");
System.out.println("Result of key is " + p.matcher("key").matches());
} else {
keyWords.add(i);
System.out.println("Added key word");
}
}
} else keyWords.add("key");
Scanner sc = new Scanner(System.in);
ArrayList<String> out = new ArrayList<String>();
while (sc.hasNextLine()) {
String s = sc.nextLine().toLowerCase();
if (!meth) {
for (String i : keyWords) {
if (s.contains(i)) {
out.add(s);
}
}
} else {
for (Pattern i : regs) {
if (i.matcher(s).matches()) {
out.add(s);
}
}
}
// if (s.contains("end")) break;
}
System.out.println(System.getProperty("line.separator") + "<---- Result");
for (String p : out) {
System.out.println(p);
}
}
}