소스 검색

tested file replace all

Eric Foster 6 년 전
부모
커밋
4c03772727
2개의 변경된 파일21개의 추가작업 그리고 3개의 파일을 삭제
  1. 14
    3
      src/main/java/HamletParser.java
  2. 7
    0
      src/test/java/HamletParserTest.java

+ 14
- 3
src/main/java/HamletParser.java 파일 보기

@@ -28,7 +28,6 @@ public class HamletParser {
28 28
                 String line = scanner.nextLine();
29 29
                 result.append(line).append("\n");
30 30
             }
31
-
32 31
             scanner.close();
33 32
         }catch(IOException e){
34 33
             e.printStackTrace();
@@ -37,12 +36,18 @@ public class HamletParser {
37 36
         return result.toString();
38 37
     }
39 38
 
39
+    public String loadFileAndReplace(String find, String replace){
40
+        Pattern pattern = createRegexPattern(find);
41
+        Matcher matcher = createMatcherFromInput(pattern, hamletData);
42
+        return findReplace(matcher, replace);
43
+    }
44
+
40 45
     public String getHamletData(){
41 46
         return hamletData;
42 47
     }
43 48
 
44
-    public Pattern createRegexPattern(String regex) {
45
-        return Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
49
+    public Pattern createRegexPattern(String find) {
50
+        return Pattern.compile(find, Pattern.CASE_INSENSITIVE);
46 51
     }
47 52
 
48 53
     public Matcher createMatcherFromInput(Pattern pattern, String input) {
@@ -52,4 +57,10 @@ public class HamletParser {
52 57
     public String findReplace(Matcher matcher, String replace) {
53 58
         return matcher.replaceAll(replace);
54 59
     }
60
+
61
+    public boolean contains(String find, String input){
62
+        Pattern pattern = createRegexPattern(find);
63
+        Matcher matcher = createMatcherFromInput(pattern, input);
64
+        return matcher.find();
65
+    }
55 66
 }

+ 7
- 0
src/test/java/HamletParserTest.java 파일 보기

@@ -17,6 +17,13 @@ public class HamletParserTest {
17 17
     }
18 18
 
19 19
     @Test
20
+    public void testLoadFileAndReplace() {
21
+        String s = hamletParser.loadFileAndReplace("Hamlet","Dolio");
22
+        boolean actual = hamletParser.contains("Hamlet","s");
23
+        assertFalse(actual);
24
+    }
25
+
26
+    @Test
20 27
     public void testChangeHamletToDolio() {
21 28
         String input = "HamletHoratioHamletHoratio";
22 29
         String find = "Hamlet";