Skip to content

Lab demonstrates regex functions. #14

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 5 commits 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
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>regex</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/HamletParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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.
Expand All @@ -10,6 +12,7 @@ public class HamletParser {
private String hamletData;

public HamletParser(){

this.hamletData = loadFile();
}

Expand All @@ -33,7 +36,53 @@ private String loadFile(){
}

public String getHamletData(){

return hamletData;
}

public String changeText(String string){
changeHamletToLeon(string);
changeHoratioToTariq(string);
return string;
}

public String changeHamletToLeon(String string) {

Pattern hamletLowerPattern = Pattern.compile("Hamlet");
Matcher hamletLowerMatcher = hamletLowerPattern.matcher(string);
String string1 = hamletLowerMatcher.replaceAll("Leon");

Pattern hamletUpperPattern = Pattern.compile("HAMLET");
Matcher hamletUpperMatcher = hamletUpperPattern.matcher(string1);
String string2 = hamletUpperMatcher.replaceAll("LEON");
return string2;

}

public String changeHoratioToTariq(String string) {


Pattern horatioLowerPattern = Pattern.compile("Horatio");
Matcher horatioLowerMatcher = horatioLowerPattern.matcher(string);
String string1 = horatioLowerMatcher.replaceAll("Tariq");

Pattern horatioUpperPattern = Pattern.compile("HORATIO");
Matcher horatioUpperMatcher = horatioUpperPattern.matcher(string1);
String string2 = horatioUpperMatcher.replaceAll("TARIQ");
return string2;

}



public boolean findMatch(String regEx, String hamletData) {

if (hamletData.contains(regEx)) return false;

else { return true; }

}



}
17 changes: 17 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 @@ -15,17 +16,33 @@ public void setUp() {

@Test
public void testChangeHamletToLeon() {

String inputTest = "Hamlet's hunt hath wrought but many a curse upon thine land. HAMLET " +
"Doth thou Hamlet haveth hatched eggs to say about these claims? Whom soever may Hamlet harm further?";

String expected = "Leon's hunt hath wrought but many a curse upon thine land. LEON " +
"Doth thou Leon haveth hatched eggs to say about these claims? Whom soever may Leon harm further?";

Assert.assertEquals(expected, hamletParser.changeHamletToLeon(inputTest));
}

@Test
public void testChangeHoratioToTariq() {

String inputTest = "HORATIO: Horatio Horatios Horatio's";
String expected = "TARIQ: Tariq Tariqs Tariq's";
Assert.assertEquals(expected, hamletParser.changeHoratioToTariq(inputTest));
}

@Test
public void testFindHoratio() {

Assert.assertTrue(hamletParser.findMatch("Horatio | HORATIO", hamletText));

}

@Test
public void testFindHamlet() {
Assert.assertTrue(hamletParser.findMatch("Hamlet| HAMLET", hamletText));
}
}
Loading