浏览代码

completed

David Thornley 6 年前
父节点
当前提交
7e2743dc6d
共有 4 个文件被更改,包括 5454 次插入0 次删除
  1. 12
    0
      pom.xml
  2. 49
    0
      src/main/java/HamletParser.java
  3. 10
    0
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml 查看文件

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>regex</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>7</source>
17
+                    <target>7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

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

@@ -1,6 +1,9 @@
1 1
 import java.io.File;
2 2
 import java.io.IOException;
3
+import java.util.ArrayList;
3 4
 import java.util.Scanner;
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
4 7
 
5 8
 /**
6 9
  * Created by thook on 10/7/15.
@@ -36,4 +39,50 @@ public class HamletParser {
36 39
         return hamletData;
37 40
     }
38 41
 
42
+    public int findHamlet() {
43
+        int hamletCount = 0;
44
+        Pattern findHamlet = Pattern.compile("\\bHamlet\\b|\\bHAMLET\\b");
45
+        Matcher hamlet = findHamlet.matcher(hamletData);
46
+        while(hamlet.find()){
47
+            hamletCount++;
48
+        }
49
+        return hamletCount;
50
+    }
51
+
52
+    public int findHoratio() {
53
+        int horatioCount = 0;
54
+        Pattern findHoratio = Pattern.compile("\\bHoratio\\b|\\bHORATIO\\b");
55
+        Matcher horatio = findHoratio.matcher(hamletData);
56
+        while(horatio.find()){
57
+            horatioCount++;
58
+        }
59
+        return horatioCount;
60
+    }
61
+
62
+    public String changeHoratioToTariq() {
63
+        Pattern findHoratio = Pattern.compile("\\bHoratio\\b");
64
+        Matcher horatio = findHoratio.matcher(hamletData);
65
+        horatio.find();
66
+        this.hamletData= horatio.replaceAll("Tariq");
67
+        Pattern findHoratio2 = Pattern.compile("\\bHORATIO\\b");
68
+        Matcher horatio2 = findHoratio2.matcher(hamletData);
69
+        horatio2.find();
70
+        this.hamletData= horatio2.replaceAll("TARIQ");
71
+
72
+        return hamletData;
73
+        }
74
+
75
+    public String changeHamletToLeon() {
76
+        Pattern findHamlet = Pattern.compile("\\Hamlet\\b");
77
+        Matcher hamlet = findHamlet.matcher(hamletData);
78
+        hamlet.find();
79
+        this.hamletData= hamlet.replaceAll("Leon");
80
+        Pattern findHamlet2 = Pattern.compile("\\HAMLET\\b");
81
+        Matcher hamlet2 = findHamlet2.matcher(hamletData);
82
+        hamlet2.find();
83
+        this.hamletData= hamlet2.replaceAll("Leon");
84
+
85
+        return hamletData;
86
+    }
39 87
 }
88
+

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

@@ -1,6 +1,8 @@
1 1
 import org.junit.Before;
2 2
 import org.junit.Test;
3 3
 
4
+import java.util.ArrayList;
5
+
4 6
 import static org.junit.Assert.*;
5 7
 
6 8
 public class HamletParserTest {
@@ -15,17 +17,25 @@ public class HamletParserTest {
15 17
 
16 18
     @Test
17 19
     public void testChangeHamletToLeon() {
20
+        String answer =hamletParser.changeHamletToLeon();
21
+        assertTrue(hamletParser.findHamlet() == 0);
18 22
     }
19 23
 
20 24
     @Test
21 25
     public void testChangeHoratioToTariq() {
26
+    String answer =hamletParser.changeHoratioToTariq();
27
+    assertTrue(hamletParser.findHoratio() == 0);
22 28
     }
23 29
 
24 30
     @Test
25 31
     public void testFindHoratio() {
32
+        assertTrue(hamletParser.findHoratio() == 158);
33
+        System.out.println(hamletParser.findHoratio());
26 34
     }
27 35
 
28 36
     @Test
29 37
     public void testFindHamlet() {
38
+        assertTrue(hamletParser.findHamlet() == 471);
39
+        System.out.println(hamletParser.findHamlet());
30 40
     }
31 41
 }

+ 5383
- 0
target/classes/hamlet.txt
文件差异内容过多而无法显示
查看文件