Skip to content
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>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
17 changes: 17 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 Down Expand Up @@ -36,4 +38,19 @@ public String getHamletData(){
return hamletData;
}

public Boolean findPattern (String hamletText, String pattern){
Pattern patternToFind = Pattern.compile(pattern);

return patternToFind.matcher(hamletText).find();
}

public String replaceString(String hamletText, String pattern, String newWord){
Pattern patternToFind = Pattern.compile(pattern);
Matcher matcher = patternToFind.matcher(hamletText);

String result = matcher.replaceAll(newWord);

return result;
}

}
2 changes: 1 addition & 1 deletion src/main/resources/hamlet.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2555,7 +2555,7 @@ Danish march. A flourish. Enter KING CLAUDIUS, QUEEN GERTRUDE, POLONIUS, OPHELIA
KING CLAUDIUS
How fares our cousin Hamlet?
HAMLET
Excellent, i' faith; of the chameleon's dish: I eat
Excellent, i' faith; of the chame's dish: I eat
the air, promise-crammed: you cannot feed capons so.
KING CLAUDIUS
I have nothing with this answer, Hamlet; these words
Expand Down
26 changes: 24 additions & 2 deletions src/test/java/HamletParserTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class HamletParserTest {
private String hamletText;
private HamletParser hamletParser;


@Before
public void setUp() {
this.hamletParser = new HamletParser();
Expand All @@ -15,17 +15,39 @@ public void setUp() {

@Test
public void testChangeHamletToLeon() {
//given
String result = hamletParser.replaceString(hamletText,"Hamlet","Leon");
//when
Boolean actualFalse = hamletParser.findPattern(result,"Hamlet");
Boolean actualTrue = hamletParser.findPattern(result,"Leon");
//then
Assert.assertFalse(actualFalse);
Assert.assertFalse(actualTrue);
}

@Test
public void testChangeHoratioToTariq() {
//given
String result = hamletParser.replaceString(hamletText,"Horatio","Tariq");
//when
Boolean actualFalse = hamletParser.findPattern(result,"Horatio");
Boolean actualTrue = hamletParser.findPattern(result,"Tariq");
//then
Assert.assertFalse(actualFalse);
Assert.assertTrue(actualTrue);
}

@Test
public void testFindHoratio() {
Boolean actual = hamletParser.findPattern(this.hamletText,"Horatio");

Assert.assertTrue(actual);
}

@Test
public void testFindHamlet() {
Boolean actual = hamletParser.findPattern(this.hamletText,"Hamlet");

Assert.assertTrue(actual);
}
}
Loading