Skip to content

lab complete #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>regex</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
66 changes: 65 additions & 1 deletion src/main/java/HamletParser.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by thook on 10/7/15.
*/
public class HamletParser {

private String hamletData;
public int counter = 0;


public HamletParser(){
this.hamletData = loadFile();
Expand All @@ -31,9 +35,69 @@ private String loadFile(){

return result.toString();
}

public String getHamletData(){
return hamletData;
}


public String changeHamletToLeon(String text) {
Pattern replace = Pattern.compile("Hamlet");
Matcher regexMatcher = replace.matcher(text);
String joe = regexMatcher.replaceAll("Leon");

Pattern replace1 = Pattern.compile("HAMLET");
Matcher regexMatcher1 = replace1.matcher(joe);
String garrett = regexMatcher1.replaceAll("LEON");

return garrett;
}


public String changeHoratioToTariq(String text) {

Pattern replace = Pattern.compile("Horatio");
Matcher regexMatcher = replace.matcher(text);
String joe = regexMatcher.replaceAll("Tariq");

Pattern replace1 = Pattern.compile("HORATIO");
Matcher regexMatcher1 = replace1.matcher(joe);
String joe1 = regexMatcher1.replaceAll("TARIQ");


return joe1;

}

public String changeAll(String text){
return changeHamletToLeon(changeHoratioToTariq(text));

}


public int findHoratio() {
return findMatch("[Hh][Oo][Rr][Aa][Tt][Ii][Oo]",hamletData);
}


public int findHamlet () {
return findMatch("[Hh][Aa][Mm][Ll][Ee][Tt]", hamletData);

}

public int findMatch(String regexStatement, String text) {
Pattern checkRegex = Pattern.compile(regexStatement);
Matcher regexMatcher = checkRegex.matcher(text);

while (regexMatcher.find()) {
counter++;
}
return counter;

}
}






124 changes: 124 additions & 0 deletions src/main/resources/Regexreasourcefile.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
public class LessonNineteen{

public static void main(String[] args){

String longString = " Derek Banas CA 12345 PA (412)555-1212 johnsmith@hotmail.com 412-555-1234 412 555-1234 ";
String strangeString = " 1Z aaa **** *** {{{ {{ { ";

/*
[ ] Insert characters that are valid
[^ ] Insert characters that are not valid
\\s Any white space
\\S Any non white space
{n,m} Whatever proceeds must occur between n and m times
*/

// Word must contain letters that are 2 to 20 characters in length
// [A-Za-z]{2,20} 0r \w{2,20}

regexChecker("\\s[A-Za-z]{2,20}\\s", longString);

/*
\\d Any digits 0-9
\\D Anything not a number
{n} Whatever proceeds must occur n times
*/

// Only 5 digits
// \\s[0-9]{5}\\s or \\d{5}

regexChecker("\\s\\d{5}\\s", longString);

/*
| Is used for OR clause situations
*/

// Must start with a A or C, followed by 1 letter in brackets
// Must be a maximum of 2 characters in length
// A[KLRZ]|C[AOT]

regexChecker("A[KLRZ]|C[AOT]", longString);

/*
{n,} Whatever proceeds must occur at least n times
+ Whatever proceeds must occur one or more times
. ^ * + ? { } [ ] \ | ( ) Characters that must be escaped or backslashed
*/

// Grab any string that contains 1 or more !

regexChecker("(\\{{1,})", strangeString);
regexChecker("(\\{+)", strangeString);

// Get anything that occurs 3 times except newline
// . Anything but newline

regexChecker(".{3}", strangeString);

/*
\\w Any word type character A-Z, a-z, 0-9, _
\\W Any non word character
* Occurs zero or more times
*/

regexChecker("\\w*", strangeString);

regexChecker("[A-Za-z0-9._\\%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}", longString);


// ? 0 or 1 of what proceeds it

regexChecker("([0-9]( |-)?)?(\\(?[0-9]{3}\\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})", longString);

regexReplace(longString);

}

public static void regexChecker(String theRegex, String str2Check){

// You define your regular expression (REGEX) using Pattern

Pattern checkRegex = Pattern.compile(theRegex);

// Creates a Matcher object that searches the String for
// anything that matches the REGEX

Matcher regexMatcher = checkRegex.matcher( str2Check );

// Cycle through the positive matches and print them to screen
// Make sure string isn't empty and trim off any whitespace

while ( regexMatcher.find() ){
if (regexMatcher.group().length() != 0){
System.out.println( regexMatcher.group().trim() );

// You can get the starting and ending indexs

System.out.println( "Start Index: " + regexMatcher.start());
System.out.println( "Start Index: " + regexMatcher.end());
}
}

System.out.println();
}

public static void regexReplace(String str2Replace){

// REGEX that matches 1 or more white space

Pattern replace = Pattern.compile("\\s+");

// This doesn't really apply, but this is how you ignore case
// Pattern replace = Pattern.compile("\\s+", Pattern.CASE_INSENSITIVE);

// trim the string t prepare it for a replace

Matcher regexMatcher = replace.matcher(str2Replace.trim());

// replaceAll replaces all white space with commas

System.out.println(regexMatcher.replaceAll(", "));

}

}
1 change: 1 addition & 0 deletions src/main/resources/testTestHamlet.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hamlet HAMLET HORATIO Horatio
45 changes: 45 additions & 0 deletions src/test/java/HamletParserTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -13,19 +14,63 @@ public void setUp() {
this.hamletText = hamletParser.getHamletData();
}



@Test
public void testChangeHamletToLeon() {
//Given
String test = "Hamlet HAMLET HORATIO. Horatio";

//When
String expected = "Leon LEON HORATIO. Horatio";
String actual = hamletParser.changeHamletToLeon(test);

Assert.assertEquals(expected,actual);


}

@Test
public void testChangeHoratioToTariq() {
//Given
String test = "Hamlet HAMLET HORATIO. Horatio";

//When
String expected = "Hamlet HAMLET TARIQ. Tariq";
String actual = hamletParser.changeHoratioToTariq(test);

Assert.assertEquals(expected,actual);


}

@Test
public void testChangeALL() {
//Given
String test = "Hamlet HAMLET HORATIO. Horatio";

//When
String expected = "Leon LEON TARIQ. Tariq";
String actual = hamletParser.changeAll(test);

Assert.assertEquals(expected, actual);
}


@Test
public void testFindHoratio() {

int expected = 158;
int actual = hamletParser.findHoratio();
Assert.assertEquals(expected,actual);

}

@Test
public void testFindHamlet() {
int expected = 472;
int actual = hamletParser.findHamlet();
Assert.assertEquals(expected,actual);

}
}
Loading