-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegexTest.java
More file actions
52 lines (44 loc) · 1.55 KB
/
RegexTest.java
File metadata and controls
52 lines (44 loc) · 1.55 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
import org.junit.Test;
import regex.NFA;
import regex.Parser;
import java.util.Scanner;
import java.util.function.Function;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class RegexTest {
@Test
public void Test() {
Function<String, Boolean> match = createMatch("(a|b)*c");
assertTrue(match.apply("aabbbc"));
assertTrue(match.apply("c"));
assertFalse(match.apply("aabbb"));
match = createMatch(".\\d{3,4}c?");
assertTrue(match.apply("w123"));
assertTrue(match.apply("c0000c"));
assertFalse(match.apply("000c"));
assertFalse(match.apply("a00ac"));
assertFalse(match.apply("b000b"));
}
public static void main(String[] args) {
repl();
}
private static void repl() {
Scanner scanner = new Scanner(System.in);
System.out.println("输入正则表达式:");
Function<String, Boolean> match = createMatch(scanner.nextLine());
while (true) {
System.out.println("输入要匹配的字符串:");
String word = scanner.nextLine();
if (word.equals("exit")) {
System.out.println("bye");
break;
}
System.out.println("匹配结果:" + match.apply(word));
}
}
public static Function<String, Boolean> createMatch(String s) {
var exp = Parser.toPostfix(Parser.insertExplicitConcatOperator(s));
NFA nfa = NFA.toNFA(exp);
return string -> NFA.search(nfa, string);
}
}