move regex module from existing module to core-java-regex module
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
### Relevant Articles:
|
||||
- [Java Timer](http://www.baeldung.com/java-timer-and-timertask)
|
||||
- [A Guide To Java Regular Expressions API](http://www.baeldung.com/regular-expressions-java)
|
||||
- [Getting Started with Java Properties](http://www.baeldung.com/java-properties)
|
||||
- [Introduction to Nashorn](http://www.baeldung.com/java-nashorn)
|
||||
- [Java Money and the Currency API](http://www.baeldung.com/java-money-and-currency)
|
||||
@@ -11,7 +10,6 @@
|
||||
- [How to Get a Name of a Method Being Executed?](http://www.baeldung.com/java-name-of-executing-method)
|
||||
- [Introduction to Java Serialization](http://www.baeldung.com/java-serialization)
|
||||
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
|
||||
- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char)
|
||||
- [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin)
|
||||
- [Quick Guide to Java Stack](http://www.baeldung.com/java-stack)
|
||||
- [Compiling Java *.class Files with javac](http://www.baeldung.com/javac)
|
||||
|
||||
@@ -1,501 +0,0 @@
|
||||
package com.baeldung.java.regex;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RegexUnitTest {
|
||||
private static Pattern pattern;
|
||||
private static Matcher matcher;
|
||||
|
||||
@Test
|
||||
public void givenText_whenSimpleRegexMatches_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("foo");
|
||||
Matcher matcher = pattern.matcher("foo");
|
||||
assertTrue(matcher.find());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenSimpleRegexMatchesTwice_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("foo");
|
||||
Matcher matcher = pattern.matcher("foofoo");
|
||||
int matches = 0;
|
||||
while (matcher.find())
|
||||
matches++;
|
||||
assertEquals(matches, 2);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenMatchesWithDotMetach_thenCorrect() {
|
||||
int matches = runTest(".", "foo");
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRepeatedText_whenMatchesOnceWithDotMetach_thenCorrect() {
|
||||
int matches = runTest("foo.", "foofoo");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenORSet_whenMatchesAny_thenCorrect() {
|
||||
int matches = runTest("[abc]", "b");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenORSet_whenMatchesAnyAndAll_thenCorrect() {
|
||||
int matches = runTest("[abc]", "cab");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenORSet_whenMatchesAllCombinations_thenCorrect() {
|
||||
int matches = runTest("[bcr]at", "bat cat rat");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNORSet_whenMatchesNon_thenCorrect() {
|
||||
int matches = runTest("[^abc]", "g");
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNORSet_whenMatchesAllExceptElements_thenCorrect() {
|
||||
int matches = runTest("[^bcr]at", "sat mat eat");
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUpperCaseRange_whenMatchesUpperCase_thenCorrect() {
|
||||
int matches = runTest("[A-Z]", "Two Uppercase alphabets 34 overall");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLowerCaseRange_whenMatchesLowerCase_thenCorrect() {
|
||||
int matches = runTest("[a-z]", "Two Uppercase alphabets 34 overall");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 26);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBothLowerAndUpperCaseRange_whenMatchesAllLetters_thenCorrect() {
|
||||
int matches = runTest("[a-zA-Z]", "Two Uppercase alphabets 34 overall");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 28);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNumberRange_whenMatchesAccurately_thenCorrect() {
|
||||
int matches = runTest("[1-5]", "Two Uppercase alphabets 34 overall");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNumberRange_whenMatchesAccurately_thenCorrect2() {
|
||||
int matches = runTest("[30-35]", "Two Uppercase alphabets 34 overall");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSets_whenMatchesUnion_thenCorrect() {
|
||||
int matches = runTest("[1-3[7-9]]", "123456789");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSets_whenMatchesIntersection_thenCorrect() {
|
||||
int matches = runTest("[1-6&&[3-9]]", "123456789");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSetWithSubtraction_whenMatchesAccurately_thenCorrect() {
|
||||
int matches = runTest("[0-9&&[^2468]]", "123456789");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDigits_whenMatches_thenCorrect() {
|
||||
int matches = runTest("\\d", "123");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonDigits_whenMatches_thenCorrect() {
|
||||
int matches = runTest("\\D", "a6c");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWhiteSpace_whenMatches_thenCorrect() {
|
||||
int matches = runTest("\\s", "a c");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonWhiteSpace_whenMatches_thenCorrect() {
|
||||
int matches = runTest("\\S", "a c");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWordCharacter_whenMatches_thenCorrect() {
|
||||
int matches = runTest("\\w", "hi!");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonWordCharacter_whenMatches_thenCorrect() {
|
||||
int matches = runTest("\\W", "hi!");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZeroOrOneQuantifier_whenMatches_thenCorrect() {
|
||||
int matches = runTest("\\a?", "hi");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZeroOrOneQuantifier_whenMatches_thenCorrect2() {
|
||||
int matches = runTest("\\a{0,1}", "hi");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZeroOrManyQuantifier_whenMatches_thenCorrect() {
|
||||
int matches = runTest("\\a*", "hi");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZeroOrManyQuantifier_whenMatches_thenCorrect2() {
|
||||
int matches = runTest("\\a{0,}", "hi");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneOrManyQuantifier_whenMatches_thenCorrect() {
|
||||
int matches = runTest("\\a+", "hi");
|
||||
assertFalse(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneOrManyQuantifier_whenMatches_thenCorrect2() {
|
||||
int matches = runTest("\\a{1,}", "hi");
|
||||
assertFalse(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBraceQuantifier_whenMatches_thenCorrect() {
|
||||
int matches = runTest("a{3}", "aaaaaa");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBraceQuantifier_whenFailsToMatch_thenCorrect() {
|
||||
int matches = runTest("a{3}", "aa");
|
||||
assertFalse(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBraceQuantifierWithRange_whenMatches_thenCorrect() {
|
||||
int matches = runTest("a{2,3}", "aaaa");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBraceQuantifierWithRange_whenMatchesLazily_thenCorrect() {
|
||||
int matches = runTest("a{2,3}?", "aaaa");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCapturingGroup_whenMatches_thenCorrect() {
|
||||
int matches = runTest("(\\d\\d)", "12");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCapturingGroup_whenMatches_thenCorrect2() {
|
||||
int matches = runTest("(\\d\\d)", "1212");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCapturingGroup_whenMatches_thenCorrect3() {
|
||||
int matches = runTest("(\\d\\d)(\\d\\d)", "1212");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCapturingGroup_whenMatchesWithBackReference_thenCorrect() {
|
||||
int matches = runTest("(\\d\\d)\\1", "1212");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCapturingGroup_whenMatchesWithBackReference_thenCorrect2() {
|
||||
int matches = runTest("(\\d\\d)\\1\\1\\1", "12121212");
|
||||
assertTrue(matches > 0);
|
||||
assertEquals(matches, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCapturingGroupAndWrongInput_whenMatchFailsWithBackReference_thenCorrect() {
|
||||
int matches = runTest("(\\d\\d)\\1", "1213");
|
||||
assertFalse(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenMatchesAtBeginning_thenCorrect() {
|
||||
int matches = runTest("^dog", "dogs are friendly");
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTextAndWrongInput_whenMatchFailsAtBeginning_thenCorrect() {
|
||||
int matches = runTest("^dog", "are dogs are friendly?");
|
||||
assertFalse(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenMatchesAtEnd_thenCorrect() {
|
||||
int matches = runTest("dog$", "Man's best friend is a dog");
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTextAndWrongInput_whenMatchFailsAtEnd_thenCorrect() {
|
||||
int matches = runTest("dog$", "is a dog man's best friend?");
|
||||
assertFalse(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenMatchesAtWordBoundary_thenCorrect() {
|
||||
int matches = runTest("\\bdog\\b", "a dog is friendly");
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenMatchesAtWordBoundary_thenCorrect2() {
|
||||
int matches = runTest("\\bdog\\b", "dog is man's best friend");
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWrongText_whenMatchFailsAtWordBoundary_thenCorrect() {
|
||||
int matches = runTest("\\bdog\\b", "snoop dogg is a rapper");
|
||||
assertFalse(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenText_whenMatchesAtWordAndNonBoundary_thenCorrect() {
|
||||
int matches = runTest("\\bdog\\B", "snoop dogg is a rapper");
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithoutCanonEq_whenMatchFailsOnEquivalentUnicode_thenCorrect() {
|
||||
int matches = runTest("\u00E9", "\u0065\u0301");
|
||||
assertFalse(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithCanonEq_whenMatchesOnEquivalentUnicode_thenCorrect() {
|
||||
int matches = runTest("\u00E9", "\u0065\u0301", Pattern.CANON_EQ);
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithDefaultMatcher_whenMatchFailsOnDifferentCases_thenCorrect() {
|
||||
int matches = runTest("dog", "This is a Dog");
|
||||
assertFalse(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithCaseInsensitiveMatcher_whenMatchesOnDifferentCases_thenCorrect() {
|
||||
int matches = runTest("dog", "This is a Dog", Pattern.CASE_INSENSITIVE);
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithEmbeddedCaseInsensitiveMatcher_whenMatchesOnDifferentCases_thenCorrect() {
|
||||
int matches = runTest("(?i)dog", "This is a Dog");
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithComments_whenMatchFailsWithoutFlag_thenCorrect() {
|
||||
int matches = runTest("dog$ #check for word dog at end of text", "This is a dog");
|
||||
assertFalse(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithComments_whenMatchesWithFlag_thenCorrect() {
|
||||
int matches = runTest("dog$ #check for word dog at end of text", "This is a dog", Pattern.COMMENTS);
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithComments_whenMatchesWithEmbeddedFlag_thenCorrect() {
|
||||
int matches = runTest("(?x)dog$ #check for word dog at end of text", "This is a dog");
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithLineTerminator_whenMatchFails_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("(.*)");
|
||||
Matcher matcher = pattern.matcher("this is a text" + System.getProperty("line.separator") + " continued on another line");
|
||||
matcher.find();
|
||||
assertEquals("this is a text", matcher.group(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithLineTerminator_whenMatchesWithDotall_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("(.*)", Pattern.DOTALL);
|
||||
Matcher matcher = pattern.matcher("this is a text" + System.getProperty("line.separator") + " continued on another line");
|
||||
matcher.find();
|
||||
assertEquals("this is a text" + System.getProperty("line.separator") + " continued on another line", matcher.group(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithLineTerminator_whenMatchesWithEmbeddedDotall_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("(?s)(.*)");
|
||||
Matcher matcher = pattern.matcher("this is a text" + System.getProperty("line.separator") + " continued on another line");
|
||||
matcher.find();
|
||||
assertEquals("this is a text" + System.getProperty("line.separator") + " continued on another line", matcher.group(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegex_whenMatchesWithoutLiteralFlag_thenCorrect() {
|
||||
int matches = runTest("(.*)", "text");
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegex_whenMatchFailsWithLiteralFlag_thenCorrect() {
|
||||
int matches = runTest("(.*)", "text", Pattern.LITERAL);
|
||||
assertFalse(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegex_whenMatchesWithLiteralFlag_thenCorrect() {
|
||||
int matches = runTest("(.*)", "text(.*)", Pattern.LITERAL);
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegex_whenMatchFailsWithoutMultilineFlag_thenCorrect() {
|
||||
int matches = runTest("dog$", "This is a dog" + System.getProperty("line.separator") + "this is a fox");
|
||||
assertFalse(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegex_whenMatchesWithMultilineFlag_thenCorrect() {
|
||||
int matches = runTest("dog$", "This is a dog" + System.getProperty("line.separator") + "this is a fox", Pattern.MULTILINE);
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegex_whenMatchesWithEmbeddedMultilineFlag_thenCorrect() {
|
||||
int matches = runTest("(?m)dog$", "This is a dog" + System.getProperty("line.separator") + "this is a fox");
|
||||
assertTrue(matches > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMatch_whenGetsIndices_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("dog");
|
||||
Matcher matcher = pattern.matcher("This dog is mine");
|
||||
matcher.find();
|
||||
assertEquals(5, matcher.start());
|
||||
assertEquals(8, matcher.end());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStudyMethodsWork_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("dog");
|
||||
Matcher matcher = pattern.matcher("dogs are friendly");
|
||||
assertTrue(matcher.lookingAt());
|
||||
assertFalse(matcher.matches());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatchesStudyMethodWorks_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("dog");
|
||||
Matcher matcher = pattern.matcher("dog");
|
||||
assertTrue(matcher.matches());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplaceFirstWorks_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("dog");
|
||||
Matcher matcher = pattern.matcher("dogs are domestic animals, dogs are friendly");
|
||||
String newStr = matcher.replaceFirst("cat");
|
||||
assertEquals("cats are domestic animals, dogs are friendly", newStr);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReplaceAllWorks_thenCorrect() {
|
||||
Pattern pattern = Pattern.compile("dog");
|
||||
Matcher matcher = pattern.matcher("dogs are domestic animals, dogs are friendly");
|
||||
String newStr = matcher.replaceAll("cat");
|
||||
assertEquals("cats are domestic animals, cats are friendly", newStr);
|
||||
|
||||
}
|
||||
|
||||
public synchronized static int runTest(String regex, String text) {
|
||||
pattern = Pattern.compile(regex);
|
||||
matcher = pattern.matcher(text);
|
||||
int matches = 0;
|
||||
while (matcher.find())
|
||||
matches++;
|
||||
return matches;
|
||||
}
|
||||
|
||||
public synchronized static int runTest(String regex, String text, int flags) {
|
||||
pattern = Pattern.compile(regex, flags);
|
||||
matcher = pattern.matcher(text);
|
||||
int matches = 0;
|
||||
while (matcher.find())
|
||||
matches++;
|
||||
return matches;
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package com.baeldung.regexp;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class EscapingCharsUnitTest {
|
||||
@Test
|
||||
public void givenRegexWithDot_whenMatchingStr_thenMatches() {
|
||||
String strInput = "foof";
|
||||
String strRegex = "foo.";
|
||||
|
||||
assertEquals(true, strInput.matches(strRegex));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithDotEsc_whenMatchingStr_thenNotMatching() {
|
||||
String strInput = "foof";
|
||||
String strRegex = "foo\\.";
|
||||
|
||||
assertEquals(false, strInput.matches(strRegex));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithPipeEscaped_whenSplitStr_thenSplits() {
|
||||
String strInput = "foo|bar|hello|world";
|
||||
String strRegex = "\\Q|\\E";
|
||||
|
||||
assertEquals(4, strInput.split(strRegex).length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRegexWithPipeEscQuoteMeth_whenSplitStr_thenSplits() {
|
||||
String strInput = "foo|bar|hello|world";
|
||||
String 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 strRegex = "$";
|
||||
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 strRegex = "\\$";
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
package com.baeldung.regexp.optmization;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class OptimizedMatcherManualTest {
|
||||
|
||||
private String action;
|
||||
|
||||
private List<String> items;
|
||||
|
||||
private class TimeWrapper {
|
||||
private long time;
|
||||
private long mstimePreCompiled;
|
||||
private long mstimeNotPreCompiled;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
Random random = new Random();
|
||||
items = new ArrayList<String>();
|
||||
long average = 0;
|
||||
|
||||
for (int i = 0; i < 100000; ++i) {
|
||||
StringBuilder s = new StringBuilder();
|
||||
int characters = random.nextInt(7) + 1;
|
||||
for (int k = 0; k < characters; ++ k) {
|
||||
char c = (char)(random.nextInt('Z' - 'A') + 'A');
|
||||
int rep = random.nextInt(95) + 5;
|
||||
for (int j = 0; j < rep; ++ j)
|
||||
s.append(c);
|
||||
average += rep;
|
||||
}
|
||||
items.add(s.toString());
|
||||
}
|
||||
|
||||
average /= 100000;
|
||||
System.out.println("generated data, average length: " + average);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenANotPreCompiledAndAPreCompiledPatternA_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
|
||||
TimeWrapper timeObj = new TimeWrapper();
|
||||
testPatterns("A*", timeObj);
|
||||
assertTrue(timeObj.mstimePreCompiled < timeObj.mstimeNotPreCompiled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenANotPreCompiledAndAPreCompiledPatternABC_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
|
||||
TimeWrapper timeObj = new TimeWrapper();
|
||||
testPatterns("A*B*C*", timeObj);
|
||||
assertTrue(timeObj.mstimePreCompiled < timeObj.mstimeNotPreCompiled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenANotPreCompiledAndAPreCompiledPatternECWF_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
|
||||
TimeWrapper timeObj = new TimeWrapper();
|
||||
testPatterns("E*C*W*F*", timeObj);
|
||||
assertTrue(timeObj.mstimePreCompiled < timeObj.mstimeNotPreCompiled);
|
||||
}
|
||||
|
||||
private void testPatterns(String regex, TimeWrapper timeObj) {
|
||||
timeObj.time = System.nanoTime();
|
||||
int matched = 0;
|
||||
int unmatched = 0;
|
||||
|
||||
for (String item : this.items) {
|
||||
if (item.matches(regex)) {
|
||||
++matched;
|
||||
}
|
||||
else {
|
||||
++unmatched;
|
||||
}
|
||||
}
|
||||
|
||||
this.action = "uncompiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched;
|
||||
|
||||
timeObj.mstimeNotPreCompiled = (System.nanoTime() - timeObj.time) / 1000000;
|
||||
System.out.println(this.action + ": " + timeObj.mstimeNotPreCompiled + "ms");
|
||||
|
||||
timeObj.time = System.nanoTime();
|
||||
|
||||
Matcher matcher = Pattern.compile(regex).matcher("");
|
||||
matched = 0;
|
||||
unmatched = 0;
|
||||
|
||||
for (String item : this.items) {
|
||||
if (matcher.reset(item).matches()) {
|
||||
++matched;
|
||||
}
|
||||
else {
|
||||
++unmatched;
|
||||
}
|
||||
}
|
||||
|
||||
this.action = "compiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched;
|
||||
|
||||
timeObj.mstimePreCompiled = (System.nanoTime() - timeObj.time) / 1000000;
|
||||
System.out.println(this.action + ": " + timeObj.mstimePreCompiled + "ms");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user