[lang] [LANG-1238] Add RegexUtils class instead of overloadinh methods in StringUtils that take a regex to take precompiled Pattern. Sort methods.
Repository: commons-lang Updated Branches: refs/heads/master bcc4f82a7 -> 54acb6e10 [LANG-1238] Add RegexUtils class instead of overloadinh methods in StringUtils that take a regex to take precompiled Pattern. Sort methods. Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/54acb6e1 Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/54acb6e1 Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/54acb6e1 Branch: refs/heads/master Commit: 54acb6e10f33d5f71770323e9769d2dcc1130a86 Parents: bcc4f82 Author: Gary GregoryAuthored: Thu May 17 17:16:08 2018 -0600 Committer: Gary Gregory Committed: Thu May 17 17:16:08 2018 -0600 -- .../apache/commons/lang3/RegExUtilsTest.java| 248 +-- 1 file changed, 124 insertions(+), 124 deletions(-) -- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/54acb6e1/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java -- diff --git a/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java b/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java index ab89f5e..7ceadfe 100644 --- a/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/RegExUtilsTest.java @@ -32,73 +32,104 @@ import org.junit.Test; public class RegExUtilsTest { @Test -public void testReplacePattern_StringStringString() { -assertNull(RegExUtils.replacePattern(null, "", "")); -assertEquals("any", RegExUtils.replacePattern("any", (String) null, "")); -assertEquals("any", RegExUtils.replacePattern("any", "", null)); +public void testRemoveAll_StringPattern() { +assertNull(RegExUtils.removeAll(null, Pattern.compile(""))); +assertEquals("any", RegExUtils.removeAll("any", (Pattern) null)); -assertEquals("zzz", RegExUtils.replacePattern("", "", "zzz")); -assertEquals("zzz", RegExUtils.replacePattern("", ".*", "zzz")); -assertEquals("", RegExUtils.replacePattern("", ".+", "zzz")); +assertEquals("any", RegExUtils.removeAll("any", Pattern.compile(""))); +assertEquals("", RegExUtils.removeAll("any", Pattern.compile(".*"))); +assertEquals("", RegExUtils.removeAll("any", Pattern.compile(".+"))); +assertEquals("", RegExUtils.removeAll("any", Pattern.compile(".?"))); -assertEquals("z", RegExUtils.replacePattern("<__>\n<__>", "<.*>", "z")); -assertEquals("z", RegExUtils.replacePattern("<__>\\n<__>", "<.*>", "z")); -assertEquals("X", RegExUtils.replacePattern("\nxy\n", ".*", "X")); +assertEquals("A\nB", RegExUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>"))); +assertEquals("AB", RegExUtils.removeAll("A<__>\n<__>B", Pattern.compile("(?s)<.*>"))); +assertEquals("ABC123", RegExUtils.removeAll("ABCabc123abc", Pattern.compile("[a-z]"))); -assertEquals("ABC___123", RegExUtils.replacePattern("ABCabc123", "[a-z]", "_")); -assertEquals("ABC_123", RegExUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "_")); -assertEquals("ABC123", RegExUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "")); -assertEquals("Lorem_ipsum_dolor_sit", -RegExUtils.replacePattern("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2")); +assertEquals("AB", RegExUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>", Pattern.DOTALL))); +assertEquals("AB", RegExUtils.removeAll("A<__>\\n<__>B", Pattern.compile("<.*>"))); +assertEquals("", RegExUtils.removeAll("x\\ny", Pattern.compile(".*"))); +assertEquals("", RegExUtils.removeAll("\nxy\n", Pattern.compile(".*", Pattern.DOTALL))); } @Test -public void testRemovePattern_StringString() { -assertNull(RegExUtils.removePattern(null, "")); -assertEquals("any", RegExUtils.removePattern("any", (String) null)); +public void testRemoveAll_StringString() { +assertNull(RegExUtils.removeAll(null, "")); +assertEquals("any", RegExUtils.removeAll("any", (String) null)); -assertEquals("", RegExUtils.removePattern("", "")); -assertEquals("", RegExUtils.removePattern("", ".*")); -assertEquals("", RegExUtils.removePattern("", ".+")); +assertEquals("any", RegExUtils.removeAll("any", "")); +assertEquals("", RegExUtils.removeAll("any", ".*")); +assertEquals("", RegExUtils.removeAll("any", ".+")); +assertEquals("", RegExUtils.removeAll("any", ".?")); -assertEquals("AB", RegExUtils.removePattern("A<__>\n<__>B", "<.*>")); -assertEquals("AB", RegExUtils.removePattern("A<__>\\n<__>B", "<.*>")); -assertEquals("",
[lang] [LANG-1238] Add RegexUtils class instead of overloadinh methods in StringUtils that take a regex to take precompiled Pattern. Sort methods.
Repository: commons-lang Updated Branches: refs/heads/master 44b6d2ead -> bcc4f82a7 [LANG-1238] Add RegexUtils class instead of overloadinh methods in StringUtils that take a regex to take precompiled Pattern. Sort methods. Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/bcc4f82a Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/bcc4f82a Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/bcc4f82a Branch: refs/heads/master Commit: bcc4f82a7aca3fe72142bd8d9ab3ac6c9d4d2767 Parents: 44b6d2e Author: Gary GregoryAuthored: Thu May 17 17:15:19 2018 -0600 Committer: Gary Gregory Committed: Thu May 17 17:15:19 2018 -0600 -- .../org/apache/commons/lang3/RegExUtils.java| 290 +-- 1 file changed, 145 insertions(+), 145 deletions(-) -- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/bcc4f82a/src/main/java/org/apache/commons/lang3/RegExUtils.java -- diff --git a/src/main/java/org/apache/commons/lang3/RegExUtils.java b/src/main/java/org/apache/commons/lang3/RegExUtils.java index 97c32fc..d148150 100644 --- a/src/main/java/org/apache/commons/lang3/RegExUtils.java +++ b/src/main/java/org/apache/commons/lang3/RegExUtils.java @@ -26,6 +26,42 @@ import java.util.regex.Pattern; public class RegExUtils { /** + * Removes each substring of the text String that matches the given regular expression pattern. + * + * This method is a {@code null} safe equivalent to: + * + * {@code pattern.matcher(text).replaceAll(StringUtils.EMPTY)} + * + * + * A {@code null} reference passed to this method is a no-op. + * + * + * StringUtils.removeAll(null, *) = null + * StringUtils.removeAll("any", (Pattern) null) = "any" + * StringUtils.removeAll("any", Pattern.compile(""))= "any" + * StringUtils.removeAll("any", Pattern.compile(".*")) = "" + * StringUtils.removeAll("any", Pattern.compile(".+")) = "" + * StringUtils.removeAll("abc", Pattern.compile(".?")) = "" + * StringUtils.removeAll("A__\n__B", Pattern.compile(".*")) = "A\nB" + * StringUtils.removeAll("A__\n__B", Pattern.compile("(?s).*")) = "AB" + * StringUtils.removeAll("A__\n__B", Pattern.compile(".*", Pattern.DOTALL)) = "AB" + * StringUtils.removeAll("ABCabc123abc", Pattern.compile("[a-z]")) = "ABC123" + * + * + * @param text text to remove from, may be null + * @param regex the regular expression to which this string is to be matched + * @return the text with any removes processed, + * {@code null} if null String input + * + * @see #replaceAll(String, Pattern, String) + * @see java.util.regex.Matcher#replaceAll(String) + * @see java.util.regex.Pattern + */ +public static String removeAll(final String text, final Pattern regex) { +return replaceAll(text, regex, StringUtils.EMPTY); +} + +/** * Removes each substring of the text String that matches the given regular expression. * * This method is a {@code null} safe equivalent to: @@ -72,39 +108,39 @@ public class RegExUtils { } /** - * Removes each substring of the text String that matches the given regular expression pattern. + * Removes the first substring of the text string that matches the given regular expression pattern. * * This method is a {@code null} safe equivalent to: * - * {@code pattern.matcher(text).replaceAll(StringUtils.EMPTY)} + * {@code pattern.matcher(text).replaceFirst(StringUtils.EMPTY)} * * * A {@code null} reference passed to this method is a no-op. * * - * StringUtils.removeAll(null, *) = null - * StringUtils.removeAll("any", (Pattern) null) = "any" - * StringUtils.removeAll("any", Pattern.compile(""))= "any" - * StringUtils.removeAll("any", Pattern.compile(".*")) = "" - * StringUtils.removeAll("any", Pattern.compile(".+")) = "" - * StringUtils.removeAll("abc", Pattern.compile(".?")) = "" - * StringUtils.removeAll("A__\n__B", Pattern.compile(".*")) = "A\nB" - * StringUtils.removeAll("A__\n__B", Pattern.compile("(?s).*")) = "AB" - * StringUtils.removeAll("A__\n__B", Pattern.compile(".*", Pattern.DOTALL)) = "AB" - * StringUtils.removeAll("ABCabc123abc", Pattern.compile("[a-z]")) = "ABC123" + * StringUtils.removeFirst(null, *) = null + * StringUtils.removeFirst("any", (Pattern) null) = "any" + * StringUtils.removeFirst("any", Pattern.compile(""))= "any" + * StringUtils.removeFirst("any", Pattern.compile(".*")) = "" + *