From 3e28582bac5f71668eb4e5b6b1302c35246dbc5f Mon Sep 17 00:00:00 2001 From: buddhini81 Date: Mon, 29 May 2017 17:47:39 +0530 Subject: [PATCH 1/3] BAEL-839 Delete as no longer needed --- .../com/baeldung/regexp/EscapingChars.java | 25 ------------------- 1 file changed, 25 deletions(-) delete mode 100644 core-java/src/main/java/com/baeldung/regexp/EscapingChars.java diff --git a/core-java/src/main/java/com/baeldung/regexp/EscapingChars.java b/core-java/src/main/java/com/baeldung/regexp/EscapingChars.java deleted file mode 100644 index 3268339a15..0000000000 --- a/core-java/src/main/java/com/baeldung/regexp/EscapingChars.java +++ /dev/null @@ -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); - } -} From 4586d471a1942834c8aaad8c0dd0681f3a91ba97 Mon Sep 17 00:00:00 2001 From: buddhini81 Date: Mon, 29 May 2017 17:55:10 +0530 Subject: [PATCH 2/3] BAEL-839 changes in all test methods --- .../baeldung/regexp/EscapingCharsTest.java | 60 ++++++++++--------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/regexp/EscapingCharsTest.java b/core-java/src/test/java/com/baeldung/regexp/EscapingCharsTest.java index 1e2293955a..49d349f69f 100644 --- a/core-java/src/test/java/com/baeldung/regexp/EscapingCharsTest.java +++ b/core-java/src/test/java/com/baeldung/regexp/EscapingCharsTest.java @@ -11,57 +11,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 = "�"; - String output = "I gave �50 to my brother.He bought candy for �35. Now he has �15 left."; - EscapingChars e = new EscapingChars(); - - assertThat(output, not(equalTo(e.changeCurrencySymbol(strInput, strRegex, strReplacement)))); + 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(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 = "�"; - String output = "I gave �50 to my brother. He bought candy for �35. Now he has �15 left."; - EscapingChars e = new EscapingChars(); - - assertEquals(output, e.changeCurrencySymbol(strInput, strRegex, strReplacement)); + 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,m.replaceAll(strReplacement)); } } From 4eeaf5b7902238ffbf76dcb3fadf632740cb1af1 Mon Sep 17 00:00:00 2001 From: buddhini81 Date: Mon, 29 May 2017 18:30:53 +0530 Subject: [PATCH 3/3] BAEL-839 - Add the missing imports --- .../src/test/java/com/baeldung/regexp/EscapingCharsTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core-java/src/test/java/com/baeldung/regexp/EscapingCharsTest.java b/core-java/src/test/java/com/baeldung/regexp/EscapingCharsTest.java index 49d349f69f..f8dbde4c4f 100644 --- a/core-java/src/test/java/com/baeldung/regexp/EscapingCharsTest.java +++ b/core-java/src/test/java/com/baeldung/regexp/EscapingCharsTest.java @@ -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;