瀏覽代碼

completely done

Tommy Rogers 6 年之前
父節點
當前提交
8d01e6ca72
共有 2 個文件被更改,包括 5 次插入5 次删除
  1. 3
    3
      src/main/java/HamletParser.java
  2. 2
    2
      src/test/java/HamletParserTest.java

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

@@ -38,15 +38,15 @@ public class HamletParser {
38 38
         return hamletData;
39 39
     }
40 40
 
41
-    public String changeHamletToLeon(String search, String replace){
42
-        Pattern patt = Pattern.compile("\\b" +search + "\\b", Pattern.CASE_INSENSITIVE);
41
+    public String changeWord(String search, String replace){
42
+        Pattern patt = Pattern.compile("^" +search + "", Pattern.CASE_INSENSITIVE);
43 43
         Matcher m = patt.matcher(hamletData);
44 44
         return m.replaceAll(replace);
45 45
     }
46 46
 
47 47
     public int findWord(CharSequence hamletData ,String search) {
48 48
         int count = 0;
49
-        Pattern patt = Pattern.compile("\\b" +search + "\\b", Pattern.CASE_INSENSITIVE);
49
+        Pattern patt = Pattern.compile("^" +search + "", Pattern.CASE_INSENSITIVE);
50 50
         Matcher m = patt.matcher(hamletData);
51 51
      while (m.find())
52 52
             count++;

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

@@ -15,7 +15,7 @@ public class HamletParserTest {
15 15
 
16 16
     @Test
17 17
     public void testChangeHamletToLeon() {
18
-        String leon = hamletParser.changeHamletToLeon("Hamlet", "Leon");
18
+        String leon = hamletParser.changeWord("Hamlet", "Leon");
19 19
 
20 20
         int expected = hamletParser.findWord(hamletText , "Hamlet");
21 21
         int actual = hamletParser.findWord(leon , "Leon");
@@ -27,7 +27,7 @@ public class HamletParserTest {
27 27
 
28 28
     @Test
29 29
     public void testChangeHoratioToTariq() {
30
-        String leon = hamletParser.changeHamletToLeon("Horatio", "Tariq");
30
+        String leon = hamletParser.changeWord("Horatio", "Tariq");
31 31
 
32 32
         int expected = hamletParser.findWord(hamletText , "Horatio");
33 33
         int actual = hamletParser.findWord(leon , "Tariq");