Quellcode durchsuchen

regex completed

Eric Foster vor 6 Jahren
Ursprung
Commit
12f8ad91c8
3 geänderte Dateien mit 5441 neuen und 5 gelöschten Zeilen
  1. 17
    1
      src/main/java/HamletParser.java
  2. 41
    4
      src/test/java/HamletParserTest.java
  3. 5383
    0
      target/classes/hamlet.txt

+ 17
- 1
src/main/java/HamletParser.java Datei anzeigen

@@ -1,6 +1,10 @@
1
+import jdk.nashorn.internal.runtime.regexp.joni.Regex;
2
+
1 3
 import java.io.File;
2 4
 import java.io.IOException;
3 5
 import java.util.Scanner;
6
+import java.util.regex.Matcher;
7
+import java.util.regex.Pattern;
4 8
 
5 9
 /**
6 10
  * Created by thook on 10/7/15.
@@ -18,7 +22,8 @@ public class HamletParser {
18 22
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19 23
         StringBuilder result = new StringBuilder("");
20 24
 
21
-        try(Scanner scanner = new Scanner(file)){
25
+        try {
26
+            Scanner scanner = new Scanner(file);
22 27
             while(scanner.hasNextLine()){
23 28
                 String line = scanner.nextLine();
24 29
                 result.append(line).append("\n");
@@ -36,4 +41,15 @@ public class HamletParser {
36 41
         return hamletData;
37 42
     }
38 43
 
44
+    public Pattern createRegexPattern(String regex) {
45
+        return Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
46
+    }
47
+
48
+    public Matcher createMatcherFromInput(Pattern pattern, String input) {
49
+        return pattern.matcher(input);
50
+    }
51
+
52
+    public String findReplace(Matcher matcher, String replace) {
53
+        return matcher.replaceAll(replace);
54
+    }
39 55
 }

+ 41
- 4
src/test/java/HamletParserTest.java Datei anzeigen

@@ -1,6 +1,9 @@
1 1
 import org.junit.Before;
2 2
 import org.junit.Test;
3 3
 
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
6
+
4 7
 import static org.junit.Assert.*;
5 8
 
6 9
 public class HamletParserTest {
@@ -14,18 +17,52 @@ public class HamletParserTest {
14 17
     }
15 18
 
16 19
     @Test
17
-    public void testChangeHamletToLeon() {
20
+    public void testChangeHamletToDolio() {
21
+        String input = "HamletHoratioHamletHoratio";
22
+        String find = "Hamlet";
23
+        String replace = "Dolio";
24
+        Pattern pattern = hamletParser.createRegexPattern(find);
25
+        Matcher matcher = hamletParser.createMatcherFromInput(pattern, input);
26
+        String actual = hamletParser.findReplace(matcher, replace);
27
+        String expected = "DolioHoratioDolioHoratio";
28
+        assertEquals(expected, actual);
18 29
     }
19 30
 
20 31
     @Test
21
-    public void testChangeHoratioToTariq() {
32
+    public void testChangeHoratioToFroilan() {
33
+        String input = "HamletHoratioHamletHoratio";
34
+        String find = "Horatio";
35
+        String replace = "Froilan";
36
+        Pattern pattern = hamletParser.createRegexPattern(find);
37
+        Matcher matcher = hamletParser.createMatcherFromInput(pattern, input);
38
+        String actual = hamletParser.findReplace(matcher, replace);
39
+        String expected = "HamletFroilanHamletFroilan";
40
+        assertEquals(expected, actual);
22 41
     }
23 42
 
24 43
     @Test
25
-    public void testFindHoratio() {
44
+    public void testCreateHoratioPattern() {
45
+        String expected = "Horatio";
46
+        Pattern pattern = hamletParser.createRegexPattern(expected);
47
+        String actual = pattern.toString();
48
+        assertEquals(expected, actual);
26 49
     }
27 50
 
28 51
     @Test
29
-    public void testFindHamlet() {
52
+    public void testCreateHamletPattern() {
53
+        String expected = "Hamlet";
54
+        Pattern pattern = hamletParser.createRegexPattern(expected);
55
+        String actual = pattern.toString();
56
+        assertEquals(expected, actual);
30 57
     }
58
+
59
+    @Test
60
+    public void testCreateMatcherFromInput(){
61
+        String input = "HamletHoratioHamletHoratio";
62
+        Pattern pattern = hamletParser.createRegexPattern("Hamlet");
63
+        Matcher matcher = hamletParser.createMatcherFromInput(pattern, input);
64
+        boolean actual = matcher instanceof Matcher;
65
+        assertTrue(actual);
66
+    }
67
+
31 68
 }

+ 5383
- 0
target/classes/hamlet.txt
Datei-Diff unterdrückt, da er zu groß ist
Datei anzeigen