Soujanya Buragapu 6 年前
父节点
当前提交
02b0e251a6
共有 5 个文件被更改,包括 5469 次插入8 次删除
  1. 1
    1
      README.md
  2. 12
    0
      pom.xml
  3. 42
    3
      src/main/java/HamletParser.java
  4. 31
    4
      src/test/java/HamletParserTest.java
  5. 5383
    0
      target/classes/hamlet.txt

+ 1
- 1
README.md 查看文件

@@ -1,4 +1,4 @@
1
-# REGEX
1
+1# REGEX
2 2
 
3 3
 ## Directions
4 4
 

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

+ 42
- 3
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.
@@ -9,11 +11,13 @@ public class HamletParser {
9 11
 
10 12
     private String hamletData;
11 13
 
12
-    public HamletParser(){
14
+    public HamletParser()
15
+    {
13 16
         this.hamletData = loadFile();
14 17
     }
15 18
 
16
-    private String loadFile(){
19
+    private String loadFile()
20
+    {
17 21
         ClassLoader classLoader = getClass().getClassLoader();
18 22
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19 23
         StringBuilder result = new StringBuilder("");
@@ -32,8 +36,43 @@ public class HamletParser {
32 36
         return result.toString();
33 37
     }
34 38
 
35
-    public String getHamletData(){
39
+    public String getHamletData()
40
+    {
36 41
         return hamletData;
37 42
     }
38 43
 
44
+    public void changeHamletToLeon() {
45
+        if(findHamlet()) {
46
+            Matcher matcher = matchingwords("Hamlet");
47
+            matcher.replaceAll("Leon");
48
+        }
49
+    }
50
+
51
+    private Matcher matchingwords(String hamlet)
52
+    {
53
+        Pattern pattern = Pattern.compile(hamlet);
54
+        Matcher matcher = pattern.matcher(this.hamletData.toLowerCase());
55
+        return  matcher;
56
+    }
57
+
58
+    public void changeHoratioToTariq() {
59
+        if(findHamlet()) {
60
+            Matcher matcher = matchingwords("Horatio");
61
+            matcher.replaceAll("Tariq");
62
+        }
63
+    }
64
+
65
+    public boolean findHoratio() {
66
+        Matcher matcher = matchingwords("horatio");
67
+        return matcher.find();
68
+    }
69
+
70
+    public boolean findHamlet() {
71
+       /* String str="((Hamlet)(.+?)";
72
+        Pattern pattern =Pattern.compile(str) ;*/
73
+        Matcher matcher = matchingwords("hamlet" );
74
+        return matcher.find();
75
+    }
39 76
 }
77
+
78
+

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

@@ -1,6 +1,10 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
7
+
4 8
 import static org.junit.Assert.*;
5 9
 
6 10
 public class HamletParserTest {
@@ -14,18 +18,41 @@ public class HamletParserTest {
14 18
     }
15 19
 
16 20
     @Test
17
-    public void testChangeHamletToLeon() {
21
+    public void testChangeHamletToLeon()
22
+    {
23
+        hamletParser.changeHamletToLeon();
24
+        boolean actual = hamletParser.findHamlet();
25
+        Assert.assertTrue(actual);
18 26
     }
19 27
 
20 28
     @Test
21
-    public void testChangeHoratioToTariq() {
29
+    public void testChangeHoratioToTariq()
30
+    {
31
+        hamletParser.changeHoratioToTariq();
32
+        boolean actual = hamletParser.findHamlet();
33
+        Assert.assertTrue(actual);
22 34
     }
23 35
 
24 36
     @Test
25
-    public void testFindHoratio() {
37
+    public void testFindHoratio()
38
+    {
39
+        hamletParser.findHoratio();
40
+        boolean actual = hamletParser.findHamlet();
41
+        Assert.assertTrue(actual);
26 42
     }
27 43
 
28 44
     @Test
29
-    public void testFindHamlet() {
45
+    public void testFindHamlet()
46
+    {
47
+//        hamletParser.findHamlet();
48
+//        boolean actual = hamletParser.findHamlet();
49
+//        Assert.assertTrue(actual);
50
+
51
+        String regex = "((Hamlet) (.+?))";
52
+        Pattern pattern = Pattern.compile(regex);
53
+        Matcher matcher = pattern.matcher(hamletText);
54
+        boolean expected = matcher.find();
55
+        System.out.println(expected);
56
+        Assert.assertTrue(expected);
30 57
     }
31 58
 }

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