瀏覽代碼

complete?

Seth 6 年之前
父節點
當前提交
07186ad465
共有 4 個文件被更改,包括 5430 次插入0 次删除
  1. 5
    0
      pom.xml
  2. 26
    0
      src/main/java/HamletParser.java
  3. 16
    0
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 5
- 0
pom.xml 查看文件

@@ -8,6 +8,11 @@
8 8
     <artifactId>regex</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <properties>
12
+        <maven.compiler.source>1.8</maven.compiler.source>
13
+        <maven.compiler.target>1.8</maven.compiler.target>
14
+    </properties>
15
+
11 16
     <dependencies>
12 17
         <dependency>
13 18
             <groupId>junit</groupId>

+ 26
- 0
src/main/java/HamletParser.java 查看文件

@@ -1,6 +1,8 @@
1 1
 import java.io.File;
2 2
 import java.io.IOException;
3 3
 import java.util.Scanner;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
4 6
 
5 7
 /**
6 8
  * Created by thook on 10/7/15.
@@ -36,4 +38,28 @@ public class HamletParser {
36 38
         return hamletData;
37 39
     }
38 40
 
41
+    public boolean findHoratio() {
42
+        Pattern pattern = Pattern.compile(("Horatio"));
43
+        Matcher matcher = pattern.matcher(hamletData);
44
+        return matcher.find();
45
+    }
46
+
47
+    public void changeHorationToTariq() {
48
+        Pattern pattern = Pattern.compile(("Horatio"));
49
+        Matcher matcher = pattern.matcher(hamletData);
50
+        hamletData = matcher.replaceAll("Tariq");
51
+    }
52
+
53
+    public boolean findHamlet() {
54
+        Pattern pattern = Pattern.compile(("Hamlet"));
55
+        Matcher matcher = pattern.matcher(hamletData);
56
+        return matcher.find();
57
+    }
58
+
59
+    public void changeHamlettToLeon() {
60
+        Pattern pattern = Pattern.compile("Hamlet");
61
+        Matcher matcher = pattern.matcher(hamletData);
62
+        hamletData = matcher.replaceAll("Leon");
63
+    }
64
+
39 65
 }

+ 16
- 0
src/test/java/HamletParserTest.java 查看文件

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
@@ -15,17 +16,32 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        hamletParser.changeHamlettToLeon();
20
+       boolean actual = hamletParser.findHamlet();
21
+
22
+
23
+        Assert.assertFalse(actual);
18 24
     }
19 25
 
20 26
     @Test
21 27
     public void testChangeHoratioToTariq() {
28
+        hamletParser.changeHorationToTariq();
29
+        boolean actual = hamletParser.findHoratio();
30
+
31
+        Assert.assertFalse(actual);
22 32
     }
23 33
 
24 34
     @Test
25 35
     public void testFindHoratio() {
36
+        boolean actual = hamletParser.findHoratio();
37
+
38
+        Assert.assertTrue(actual);
26 39
     }
27 40
 
28 41
     @Test
29 42
     public void testFindHamlet() {
43
+        boolean actual = hamletParser.findHamlet();
44
+
45
+        Assert.assertTrue(actual);
30 46
     }
31 47
 }

+ 5383
- 0
target/classes/hamlet.txt
文件差異過大導致無法顯示
查看文件