Quellcode durchsuchen

Merge 0f963a3f06a21cda2f6f11f116660ec76a3670a0 into 99c0c83b2f011a8e58c86fca53df6bfa67c86565

danzcw vor 6 Jahren
Ursprung
Commit
13342f04a4
Es ist kein Account mit dieser Commiter-Email verbunden

+ 12
- 0
pom.xml Datei anzeigen

@@ -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>1.7</source>
17
+                    <target>1.7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

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

@@ -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.
@@ -8,6 +10,8 @@ import java.util.Scanner;
8 10
 public class HamletParser {
9 11
 
10 12
     private String hamletData;
13
+    public int counter = 0;
14
+
11 15
 
12 16
     public HamletParser(){
13 17
         this.hamletData = loadFile();
@@ -31,9 +35,69 @@ public class HamletParser {
31 35
 
32 36
         return result.toString();
33 37
     }
34
-
35 38
     public String getHamletData(){
36 39
         return hamletData;
37 40
     }
38 41
 
42
+
43
+    public String changeHamletToLeon(String text) {
44
+        Pattern replace = Pattern.compile("Hamlet");
45
+        Matcher regexMatcher = replace.matcher(text);
46
+        String joe = regexMatcher.replaceAll("Leon");
47
+
48
+        Pattern replace1 = Pattern.compile("HAMLET");
49
+        Matcher regexMatcher1 = replace1.matcher(joe);
50
+        String garrett = regexMatcher1.replaceAll("LEON");
51
+
52
+        return garrett;
53
+    }
54
+
55
+
56
+    public String changeHoratioToTariq(String text) {
57
+
58
+        Pattern replace = Pattern.compile("Horatio");
59
+        Matcher regexMatcher = replace.matcher(text);
60
+        String joe = regexMatcher.replaceAll("Tariq");
61
+
62
+        Pattern replace1 = Pattern.compile("HORATIO");
63
+        Matcher regexMatcher1 = replace1.matcher(joe);
64
+        String joe1 = regexMatcher1.replaceAll("TARIQ");
65
+
66
+
67
+        return joe1;
68
+
69
+    }
70
+
71
+    public String changeAll(String text){
72
+       return changeHamletToLeon(changeHoratioToTariq(text));
73
+
74
+    }
75
+
76
+
77
+    public int findHoratio() {
78
+        return findMatch("[Hh][Oo][Rr][Aa][Tt][Ii][Oo]",hamletData);
79
+        }
80
+
81
+
82
+    public int findHamlet () {
83
+        return findMatch("[Hh][Aa][Mm][Ll][Ee][Tt]", hamletData);
84
+
85
+    }
86
+
87
+    public int findMatch(String regexStatement, String text) {
88
+        Pattern checkRegex = Pattern.compile(regexStatement);
89
+        Matcher regexMatcher = checkRegex.matcher(text);
90
+
91
+        while (regexMatcher.find()) {
92
+            counter++;
93
+        }
94
+        return counter;
95
+
96
+    }
39 97
 }
98
+
99
+
100
+
101
+
102
+
103
+

+ 124
- 0
src/main/resources/Regexreasourcefile.text Datei anzeigen

@@ -0,0 +1,124 @@
1
+public class LessonNineteen{
2
+	
3
+	public static void main(String[] args){
4
+		
5
+		String longString = " Derek Banas CA 12345 PA (412)555-1212 johnsmith@hotmail.com 412-555-1234 412 555-1234 "; 
6
+		String strangeString = " 1Z aaa **** *** {{{ {{ { ";
7
+		
8
+		/*
9
+		[ ]  Insert characters that are valid
10
+		[^ ]  Insert characters that are not valid
11
+		\\s  Any white space
12
+		\\S  Any non white space
13
+		{n,m}  Whatever proceeds must occur between n and m times
14
+		*/
15
+		
16
+		// Word must contain letters that are 2 to 20 characters in length
17
+		// [A-Za-z]{2,20} 0r \w{2,20}
18
+		
19
+		regexChecker("\\s[A-Za-z]{2,20}\\s", longString);
20
+		
21
+		/*
22
+		\\d  Any digits 0-9
23
+	 	\\D  Anything not a number
24
+	 	{n}  Whatever proceeds must occur n times
25
+	 	*/
26
+		
27
+		// Only 5 digits
28
+		// \\s[0-9]{5}\\s or \\d{5}
29
+		
30
+		regexChecker("\\s\\d{5}\\s", longString);
31
+		
32
+		/*
33
+		|  Is used for OR clause situations
34
+		*/
35
+		
36
+		// Must start with a A or C, followed by 1 letter in brackets
37
+		// Must be a maximum of 2 characters in length
38
+		// A[KLRZ]|C[AOT]
39
+		
40
+		regexChecker("A[KLRZ]|C[AOT]", longString);
41
+		
42
+		/*
43
+		{n,}  Whatever proceeds must occur at least n times
44
+		+  Whatever proceeds must occur one or more times
45
+		. ^ * + ? { } [ ] \ | ( )  Characters that must be escaped or backslashed
46
+		*/
47
+		
48
+		// Grab any string that contains 1 or more !
49
+		
50
+		regexChecker("(\\{{1,})", strangeString);
51
+		regexChecker("(\\{+)", strangeString);
52
+		
53
+		// Get anything that occurs 3 times except newline
54
+		// .  Anything but newline
55
+		
56
+		regexChecker(".{3}", strangeString);
57
+		
58
+		/*
59
+		\\w  Any word type character A-Z, a-z, 0-9, _
60
+		\\W  Any non word character
61
+		*  Occurs zero or more times
62
+		*/
63
+		
64
+		regexChecker("\\w*", strangeString);
65
+		
66
+		regexChecker("[A-Za-z0-9._\\%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}", longString);
67
+		
68
+		
69
+		// ?  0 or 1 of what proceeds it
70
+		
71
+		regexChecker("([0-9]( |-)?)?(\\(?[0-9]{3}\\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})", longString);
72
+		
73
+		regexReplace(longString);
74
+		
75
+	}
76
+	
77
+	public static void regexChecker(String theRegex, String str2Check){
78
+		
79
+		// You define your regular expression (REGEX) using Pattern
80
+		
81
+		Pattern checkRegex = Pattern.compile(theRegex);
82
+		
83
+		// Creates a Matcher object that searches the String for
84
+		// anything that matches the REGEX
85
+		
86
+		Matcher regexMatcher = checkRegex.matcher( str2Check );
87
+		
88
+		// Cycle through the positive matches and print them to screen
89
+		// Make sure string isn't empty and trim off any whitespace
90
+		
91
+		while ( regexMatcher.find() ){
92
+			if (regexMatcher.group().length() != 0){
93
+				System.out.println( regexMatcher.group().trim() );
94
+				
95
+				// You can get the starting and ending indexs
96
+				
97
+				System.out.println( "Start Index: " + regexMatcher.start());
98
+				System.out.println( "Start Index: " + regexMatcher.end());
99
+			}
100
+		}
101
+		
102
+		System.out.println();
103
+	}
104
+	
105
+	public static void regexReplace(String str2Replace){
106
+		
107
+		// REGEX that matches 1 or more white space
108
+		
109
+		Pattern replace = Pattern.compile("\\s+");
110
+		
111
+		// This doesn't really apply, but this is how you ignore case
112
+		// Pattern replace = Pattern.compile("\\s+", Pattern.CASE_INSENSITIVE);
113
+		
114
+		// trim the string t prepare it for a replace
115
+		
116
+		Matcher regexMatcher = replace.matcher(str2Replace.trim());
117
+		
118
+		// replaceAll replaces all white space with commas
119
+		
120
+		System.out.println(regexMatcher.replaceAll(", "));
121
+		
122
+	}
123
+	
124
+}

+ 1
- 0
src/main/resources/testTestHamlet.txt Datei anzeigen

@@ -0,0 +1 @@
1
+Hamlet HAMLET HORATIO Horatio

+ 45
- 0
src/test/java/HamletParserTest.java Datei anzeigen

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
@@ -13,19 +14,63 @@ public class HamletParserTest {
13 14
         this.hamletText = hamletParser.getHamletData();
14 15
     }
15 16
 
17
+
18
+
16 19
     @Test
17 20
     public void testChangeHamletToLeon() {
21
+        //Given
22
+        String test = "Hamlet HAMLET HORATIO. Horatio";
23
+
24
+        //When
25
+        String expected = "Leon LEON HORATIO. Horatio";
26
+        String actual = hamletParser.changeHamletToLeon(test);
27
+
28
+        Assert.assertEquals(expected,actual);
29
+
30
+
18 31
     }
19 32
 
20 33
     @Test
21 34
     public void testChangeHoratioToTariq() {
35
+        //Given
36
+        String test = "Hamlet HAMLET HORATIO. Horatio";
37
+
38
+        //When
39
+        String expected = "Hamlet HAMLET TARIQ. Tariq";
40
+        String actual = hamletParser.changeHoratioToTariq(test);
41
+
42
+        Assert.assertEquals(expected,actual);
43
+
44
+
22 45
     }
23 46
 
24 47
     @Test
48
+    public void testChangeALL() {
49
+        //Given
50
+        String test = "Hamlet HAMLET HORATIO. Horatio";
51
+
52
+        //When
53
+        String expected = "Leon LEON TARIQ. Tariq";
54
+        String actual = hamletParser.changeAll(test);
55
+
56
+        Assert.assertEquals(expected, actual);
57
+    }
58
+
59
+
60
+        @Test
25 61
     public void testFindHoratio() {
62
+
63
+        int expected = 158;
64
+        int actual = hamletParser.findHoratio();
65
+        Assert.assertEquals(expected,actual);
66
+
26 67
     }
27 68
 
28 69
     @Test
29 70
     public void testFindHamlet() {
71
+        int expected = 472;
72
+        int actual = hamletParser.findHamlet();
73
+        Assert.assertEquals(expected,actual);
74
+
30 75
     }
31 76
 }

+ 124
- 0
target/classes/Regexreasourcefile.text Datei anzeigen

@@ -0,0 +1,124 @@
1
+public class LessonNineteen{
2
+	
3
+	public static void main(String[] args){
4
+		
5
+		String longString = " Derek Banas CA 12345 PA (412)555-1212 johnsmith@hotmail.com 412-555-1234 412 555-1234 "; 
6
+		String strangeString = " 1Z aaa **** *** {{{ {{ { ";
7
+		
8
+		/*
9
+		[ ]  Insert characters that are valid
10
+		[^ ]  Insert characters that are not valid
11
+		\\s  Any white space
12
+		\\S  Any non white space
13
+		{n,m}  Whatever proceeds must occur between n and m times
14
+		*/
15
+		
16
+		// Word must contain letters that are 2 to 20 characters in length
17
+		// [A-Za-z]{2,20} 0r \w{2,20}
18
+		
19
+		regexChecker("\\s[A-Za-z]{2,20}\\s", longString);
20
+		
21
+		/*
22
+		\\d  Any digits 0-9
23
+	 	\\D  Anything not a number
24
+	 	{n}  Whatever proceeds must occur n times
25
+	 	*/
26
+		
27
+		// Only 5 digits
28
+		// \\s[0-9]{5}\\s or \\d{5}
29
+		
30
+		regexChecker("\\s\\d{5}\\s", longString);
31
+		
32
+		/*
33
+		|  Is used for OR clause situations
34
+		*/
35
+		
36
+		// Must start with a A or C, followed by 1 letter in brackets
37
+		// Must be a maximum of 2 characters in length
38
+		// A[KLRZ]|C[AOT]
39
+		
40
+		regexChecker("A[KLRZ]|C[AOT]", longString);
41
+		
42
+		/*
43
+		{n,}  Whatever proceeds must occur at least n times
44
+		+  Whatever proceeds must occur one or more times
45
+		. ^ * + ? { } [ ] \ | ( )  Characters that must be escaped or backslashed
46
+		*/
47
+		
48
+		// Grab any string that contains 1 or more !
49
+		
50
+		regexChecker("(\\{{1,})", strangeString);
51
+		regexChecker("(\\{+)", strangeString);
52
+		
53
+		// Get anything that occurs 3 times except newline
54
+		// .  Anything but newline
55
+		
56
+		regexChecker(".{3}", strangeString);
57
+		
58
+		/*
59
+		\\w  Any word type character A-Z, a-z, 0-9, _
60
+		\\W  Any non word character
61
+		*  Occurs zero or more times
62
+		*/
63
+		
64
+		regexChecker("\\w*", strangeString);
65
+		
66
+		regexChecker("[A-Za-z0-9._\\%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}", longString);
67
+		
68
+		
69
+		// ?  0 or 1 of what proceeds it
70
+		
71
+		regexChecker("([0-9]( |-)?)?(\\(?[0-9]{3}\\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})", longString);
72
+		
73
+		regexReplace(longString);
74
+		
75
+	}
76
+	
77
+	public static void regexChecker(String theRegex, String str2Check){
78
+		
79
+		// You define your regular expression (REGEX) using Pattern
80
+		
81
+		Pattern checkRegex = Pattern.compile(theRegex);
82
+		
83
+		// Creates a Matcher object that searches the String for
84
+		// anything that matches the REGEX
85
+		
86
+		Matcher regexMatcher = checkRegex.matcher( str2Check );
87
+		
88
+		// Cycle through the positive matches and print them to screen
89
+		// Make sure string isn't empty and trim off any whitespace
90
+		
91
+		while ( regexMatcher.find() ){
92
+			if (regexMatcher.group().length() != 0){
93
+				System.out.println( regexMatcher.group().trim() );
94
+				
95
+				// You can get the starting and ending indexs
96
+				
97
+				System.out.println( "Start Index: " + regexMatcher.start());
98
+				System.out.println( "Start Index: " + regexMatcher.end());
99
+			}
100
+		}
101
+		
102
+		System.out.println();
103
+	}
104
+	
105
+	public static void regexReplace(String str2Replace){
106
+		
107
+		// REGEX that matches 1 or more white space
108
+		
109
+		Pattern replace = Pattern.compile("\\s+");
110
+		
111
+		// This doesn't really apply, but this is how you ignore case
112
+		// Pattern replace = Pattern.compile("\\s+", Pattern.CASE_INSENSITIVE);
113
+		
114
+		// trim the string t prepare it for a replace
115
+		
116
+		Matcher regexMatcher = replace.matcher(str2Replace.trim());
117
+		
118
+		// replaceAll replaces all white space with commas
119
+		
120
+		System.out.println(regexMatcher.replaceAll(", "));
121
+		
122
+	}
123
+	
124
+}

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


+ 1
- 0
target/classes/testTestHamlet.txt Datei anzeigen

@@ -0,0 +1 @@
1
+Hamlet HAMLET HORATIO Horatio