Merge pull request #1947 from buddhini81/master

Changes done for BAEL-839 - Remove EscapingChars class and changes done in EscapingCharsTest
This commit is contained in:
slavisa-baeldung 2017-05-29 20:33:05 +02:00 committed by GitHub
commit 689a72ddf2
2 changed files with 33 additions and 54 deletions

View File

@ -1,25 +0,0 @@
package com.baeldung.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EscapingChars {
public boolean isMatching(String input, String pattern) {
return input.matches(pattern);
}
public int splitAndCountWords(String input, String pattern) {
return input.split(pattern).length;
}
public int splitAndCountWordsUsingQuoteMethod(String input, String pattern) {
return input.split(Pattern.quote(pattern)).length;
}
public String changeCurrencySymbol(String input, String pattern,
String correctStr) {
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(input);
return m.replaceAll(correctStr);
}
}

View File

@ -3,6 +3,8 @@ package com.baeldung.regexp;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
@ -11,57 +13,59 @@ public class EscapingCharsTest {
public void givenRegexWithDot_whenMatchingStr_thenMatches() {
String strInput = "foof";
String strRegex = "foo.";
EscapingChars e = new EscapingChars();
assertEquals(true, e.isMatching(strInput, strRegex));
assertEquals(true, strInput.matches(strRegex));
}
@Test
public void givenRegexWithDotEsc_whenMatchingStr_thenNotMatching() {
String strInput = "foof";
String strRegex = "foo\\.";
EscapingChars e = new EscapingChars();
assertEquals(false, e.isMatching(strInput, strRegex));
assertEquals(false, strInput.matches(strRegex));
}
@Test
public void givenRegexWithPipeEscaped_whenSplitStr_thenSplits() {
String strInput = "foo|bar|hello|world";
String strRegex = "\\Q|\\E";
EscapingChars e = new EscapingChars();
assertEquals(4, e.splitAndCountWords(strInput, strRegex));
assertEquals(4, strInput.split(strRegex).length);
}
@Test
public void givenRegexWithPipeEscQuoteMeth_whenSplitStr_thenSplits() {
String strInput = "foo|bar|hello|world";
String strRegex = "|";
EscapingChars e = new EscapingChars();
assertEquals(4, e.splitAndCountWordsUsingQuoteMethod(strInput, strRegex));
assertEquals(4,strInput.split(Pattern.quote(strRegex)).length);
}
@Test
public void givenRegexWithDollar_whenReplacing_thenNotReplace() {
String strInput = "I gave $50 to my brother.He bought candy for $35. Now he has $15 left.";
String strInput = "I gave $50 to my brother."
+ "He bought candy for $35. Now he has $15 left.";
String strRegex = "$";
String strReplacement = "<EFBFBD>";
String output = "I gave <20>50 to my brother.He bought candy for <20>35. Now he has <20>15 left.";
EscapingChars e = new EscapingChars();
String strReplacement = "£";
String output = "I gave £50 to my brother."
+ "He bought candy for £35. Now he has £15 left.";
Pattern p = Pattern.compile(strRegex);
Matcher m = p.matcher(strInput);
assertThat(output, not(equalTo(e.changeCurrencySymbol(strInput, strRegex, strReplacement))));
assertThat(output, not(equalTo(m.replaceAll(strReplacement))));
}
@Test
public void givenRegexWithDollarEsc_whenReplacing_thenReplace() {
String strInput = "I gave $50 to my brother. He bought candy for $35. Now he has $15 left.";
String strInput = "I gave $50 to my brother."
+ "He bought candy for $35. Now he has $15 left.";
String strRegex = "\\$";
String strReplacement = "<EFBFBD>";
String output = "I gave <20>50 to my brother. He bought candy for <20>35. Now he has <20>15 left.";
EscapingChars e = new EscapingChars();
String strReplacement = "£";
String output = "I gave £50 to my brother."
+ "He bought candy for £35. Now he has £15 left.";
Pattern p = Pattern.compile(strRegex);
Matcher m = p.matcher(strInput);
assertEquals(output, e.changeCurrencySymbol(strInput, strRegex, strReplacement));
assertEquals(output,m.replaceAll(strReplacement));
}
}