Skip to content

Commit 689a72d

Browse files
Merge pull request eugenp#1947 from buddhini81/master
Changes done for BAEL-839 - Remove EscapingChars class and changes done in EscapingCharsTest
2 parents 8f3039b + 4eeaf5b commit 689a72d

2 files changed

Lines changed: 33 additions & 54 deletions

File tree

‎core-java/src/main/java/com/baeldung/regexp/EscapingChars.java‎

Lines changed: 0 additions & 25 deletions
This file was deleted.

‎core-java/src/test/java/com/baeldung/regexp/EscapingCharsTest.java‎

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static junit.framework.TestCase.assertEquals;
44
import static org.junit.Assert.assertThat;
55
import static org.hamcrest.CoreMatchers.*;
6+
import java.util.regex.Matcher;
7+
import java.util.regex.Pattern;
68

79
import org.junit.Test;
810

@@ -11,57 +13,59 @@ public class EscapingCharsTest {
1113
public void givenRegexWithDot_whenMatchingStr_thenMatches() {
1214
String strInput = "foof";
1315
String strRegex = "foo.";
14-
EscapingChars e = new EscapingChars();
15-
16-
assertEquals(true, e.isMatching(strInput, strRegex));
16+
17+
assertEquals(true, strInput.matches(strRegex));
1718
}
18-
19+
1920
@Test
2021
public void givenRegexWithDotEsc_whenMatchingStr_thenNotMatching() {
2122
String strInput = "foof";
2223
String strRegex = "foo\\.";
23-
EscapingChars e = new EscapingChars();
24-
25-
assertEquals(false, e.isMatching(strInput, strRegex));
24+
25+
assertEquals(false, strInput.matches(strRegex));
2626
}
27-
27+
2828
@Test
2929
public void givenRegexWithPipeEscaped_whenSplitStr_thenSplits() {
3030
String strInput = "foo|bar|hello|world";
3131
String strRegex = "\\Q|\\E";
32-
EscapingChars e = new EscapingChars();
33-
34-
assertEquals(4, e.splitAndCountWords(strInput, strRegex));
32+
33+
assertEquals(4, strInput.split(strRegex).length);
3534
}
36-
35+
3736
@Test
3837
public void givenRegexWithPipeEscQuoteMeth_whenSplitStr_thenSplits() {
3938
String strInput = "foo|bar|hello|world";
4039
String strRegex = "|";
41-
EscapingChars e = new EscapingChars();
42-
43-
assertEquals(4, e.splitAndCountWordsUsingQuoteMethod(strInput, strRegex));
40+
41+
assertEquals(4,strInput.split(Pattern.quote(strRegex)).length);
4442
}
45-
43+
4644
@Test
4745
public void givenRegexWithDollar_whenReplacing_thenNotReplace() {
48-
String strInput = "I gave $50 to my brother.He bought candy for $35. Now he has $15 left.";
46+
String strInput = "I gave $50 to my brother."
47+
+ "He bought candy for $35. Now he has $15 left.";
4948
String strRegex = "$";
50-
String strReplacement = "�";
51-
String output = "I gave �50 to my brother.He bought candy for �35. Now he has �15 left.";
52-
EscapingChars e = new EscapingChars();
53-
54-
assertThat(output, not(equalTo(e.changeCurrencySymbol(strInput, strRegex, strReplacement))));
49+
String strReplacement = "£";
50+
String output = "I gave £50 to my brother."
51+
+ "He bought candy for £35. Now he has £15 left.";
52+
Pattern p = Pattern.compile(strRegex);
53+
Matcher m = p.matcher(strInput);
54+
55+
assertThat(output, not(equalTo(m.replaceAll(strReplacement))));
5556
}
56-
57+
5758
@Test
5859
public void givenRegexWithDollarEsc_whenReplacing_thenReplace() {
59-
String strInput = "I gave $50 to my brother. He bought candy for $35. Now he has $15 left.";
60+
String strInput = "I gave $50 to my brother."
61+
+ "He bought candy for $35. Now he has $15 left.";
6062
String strRegex = "\\$";
61-
String strReplacement = "�";
62-
String output = "I gave �50 to my brother. He bought candy for �35. Now he has �15 left.";
63-
EscapingChars e = new EscapingChars();
64-
65-
assertEquals(output, e.changeCurrencySymbol(strInput, strRegex, strReplacement));
63+
String strReplacement = "£";
64+
String output = "I gave £50 to my brother."
65+
+ "He bought candy for £35. Now he has £15 left.";
66+
Pattern p = Pattern.compile(strRegex);
67+
Matcher m = p.matcher(strInput);
68+
69+
assertEquals(output,m.replaceAll(strReplacement));
6670
}
6771
}

0 commit comments

Comments
 (0)