-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.java
41 lines (31 loc) · 1.01 KB
/
Test.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
37
38
39
40
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.List;
public class Test {
public static void main(String[] args) throws Exception {
String message = getFirstLine("message.txt");
String pattern = getFirstLine("pattern.txt");
regex_run(pattern, message);
}
public static String getFirstLine(String path) {
try {
List<String> allLines = Files.readAllLines(Paths.get(path));
return allLines.get(0);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void regex_run(String pattern, String message) throws Exception {
Pattern p = Pattern.compile(pattern);
Long start = System.currentTimeMillis();
Matcher m = p.matcher(message);
boolean matches = m.find();
Long timeElapsed = System.currentTimeMillis() - start;
String result = String.format("Took %dms, %s", timeElapsed, matches ? "matching" : "non matching");
System.out.println(result);
}
}