Trinh Tong преди 6 години
родител
ревизия
908b9db839
променени са 3 файла, в които са добавени 50 реда и са изтрити 0 реда
  1. 12
    0
      pom.xml
  2. 15
    0
      src/main/java/HamletParser.java
  3. 23
    0
      src/test/java/HamletParserTest.java

+ 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>

+ 15
- 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,17 @@ public class HamletParser {
36 38
         return hamletData;
37 39
     }
38 40
 
41
+    public String searchAndReplace(Pattern pattern, String toWhat) {
42
+        String newHam = getHamletData();
43
+        Matcher matcher = pattern.matcher(newHam);
44
+
45
+        return matcher.replaceAll(toWhat);
46
+    }
47
+
48
+    public boolean findMe(Pattern patter, String text) {
49
+        Matcher matcher = patter.matcher(text);
50
+
51
+        return matcher.matches();
52
+    }
53
+
39 54
 }

+ 23
- 0
src/test/java/HamletParserTest.java Целия файл

@@ -1,6 +1,9 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
5
+import java.util.regex.Pattern;
6
+
4 7
 import static org.junit.Assert.*;
5 8
 
6 9
 public class HamletParserTest {
@@ -15,17 +18,37 @@ public class HamletParserTest {
15 18
 
16 19
     @Test
17 20
     public void testChangeHamletToLeon() {
21
+        Pattern ham = Pattern.compile("(H|h)(A|a)(M|m)(L|l)(E|e)(T|t)");
22
+        String output = hamletParser.searchAndReplace(ham, "Leon");
23
+
24
+        Assert.assertFalse(output.contains("Hamlet"));
25
+        Assert.assertFalse(output.contains("HAMLET"));
26
+
18 27
     }
19 28
 
20 29
     @Test
21 30
     public void testChangeHoratioToTariq() {
31
+        Pattern tariq = Pattern.compile("(H|h)(O|o)(R|r)(A|a)(T|t)(I|i)(O|o)");
32
+        String output = hamletParser.searchAndReplace(tariq, "Tariq");
33
+
34
+        Assert.assertFalse(output.contains("Horatio"));
35
+        Assert.assertFalse(output.contains("HORATIO"));
22 36
     }
23 37
 
24 38
     @Test
25 39
     public void testFindHoratio() {
40
+        Pattern horatio = Pattern.compile("(H|h)(O|o)(R|r)(A|a)(T|t)(I|i)(O|o)");
41
+        String output = hamletParser.searchAndReplace(horatio, "Tariq");
42
+
43
+        Assert.assertFalse(hamletParser.findMe(horatio, output));
26 44
     }
27 45
 
28 46
     @Test
29 47
     public void testFindHamlet() {
48
+        Pattern hamlet = Pattern.compile("(H|h)(A|a)(M|m)(L|l)(E|e)(T|t)");
49
+        String output = hamletParser.searchAndReplace(hamlet, "Leon");
50
+
51
+        Assert.assertFalse(hamletParser.findMe(hamlet, output));
52
+
30 53
     }
31 54
 }