You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lg...@apache.org on 2015/11/05 14:21:55 UTC

[1/3] [lang] LANG-1185 Add remove by regular expression methods in StringUtils : - String StringUtils.removeAll(String text, String regex); - String StringUtils.removeFirst(String text, String regex);

Repository: commons-lang
Updated Branches:
  refs/heads/master 94184ce38 -> a5650a167


LANG-1185 Add remove by regular expression methods in StringUtils :
  - String StringUtils.removeAll(String text, String regex);
  - String StringUtils.removeFirst(String text, String regex);


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/b4842f55
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/b4842f55
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/b4842f55

Branch: refs/heads/master
Commit: b4842f559f0d8b8b5a98a19b1f3795a42c1a2614
Parents: 94184ce
Author: Loic Guibert <lg...@apache.org>
Authored: Thu Nov 5 17:14:11 2015 +0400
Committer: Loic Guibert <lg...@apache.org>
Committed: Thu Nov 5 17:14:11 2015 +0400

----------------------------------------------------------------------
 .../org/apache/commons/lang3/StringUtils.java   | 93 ++++++++++++++++++++
 .../apache/commons/lang3/StringUtilsTest.java   | 45 ++++++++++
 2 files changed, 138 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/b4842f55/src/main/java/org/apache/commons/lang3/StringUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 7bf62d1..d83f28b 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -4641,6 +4641,99 @@ public class StringUtils {
         return new String(chars, 0, pos);
     }
 
+    /**
+     * <p>Removes each substring of the text String that matches the given regular expression.</p>
+     *
+     * This method is a {@code null} safe equivalent to:
+     * <ul>
+     *  <li>{@code text.replaceAll(regex, StringUtils.EMPTY)}</li>
+     *  <li>{@code Pattern.compile(regex).matcher(text).replaceAll(StringUtils.EMPTY)}</li>
+     * </ul>
+     *
+     * <p>A {@code null} reference passed to this method is a no-op.</p>
+     *
+     * <p>Unlike in the {@link #removePattern(String, String)} method, the {@link Pattern#DOTALL} option
+     * is NOT automatically added.
+     * To use the DOTALL option prepend <code>"(?s)"</code> to the regex.
+     * DOTALL is also know as single-line mode in Perl.</p>
+     *
+     * <pre>
+     * StringUtils.removeAll(null, *)      = null
+     * StringUtils.removeAll("any", null)  = "any"
+     * StringUtils.removeAll("any", "")    = "any"
+     * StringUtils.removeAll("any", ".*")  = ""
+     * StringUtils.removeAll("any", ".+")  = ""
+     * StringUtils.removeAll("abc", ".?")  = ""
+     * StringUtils.removeAll("A<__>\n<__>B", "<.*>")      = "A\nB"
+     * StringUtils.removeAll("A<__>\n<__>B", "(?s)<.*>")  = "AB"
+     * StringUtils.removeAll("ABCabc123abc", "[a-z]")     = "ABC123"
+     * </pre>
+     *
+     * @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
+     *
+     * @throws  java.util.regex.PatternSyntaxException
+     *              if the regular expression's syntax is invalid
+     *
+     * @see #replaceAll(String, String, String)
+     * @see #removePattern(String, String)
+     * @see String#replaceAll(String, String)
+     * @see java.util.regex.Pattern
+     * @see java.util.regex.Pattern#DOTALL
+     * @since 3.5
+     */
+    public static String removeAll(final String text, final String regex) {
+        return replaceAll(text, regex, StringUtils.EMPTY);
+    }
+
+    /**
+     * <p>Removes the first substring of the text string that matches the given regular expression.</p>
+     *
+     * This method is a {@code null} safe equivalent to:
+     * <ul>
+     *  <li>{@code text.replaceFirst(regex, StringUtils.EMPTY)}</li>
+     *  <li>{@code Pattern.compile(regex).matcher(text).replaceFirst(StringUtils.EMPTY)}</li>
+     * </ul>
+     *
+     * <p>A {@code null} reference passed to this method is a no-op.</p>
+     *
+     * <p>The {@link Pattern#DOTALL} option is NOT automatically added.
+     * To use the DOTALL option prepend <code>"(?s)"</code> to the regex.
+     * DOTALL is also know as single-line mode in Perl.</p>
+     *
+     * <pre>
+     * StringUtils.removeFirst(null, *)      = null
+     * StringUtils.removeFirst("any", null)  = "any"
+     * StringUtils.removeFirst("any", "")    = "any"
+     * StringUtils.removeFirst("any", ".*")  = ""
+     * StringUtils.removeFirst("any", ".+")  = ""
+     * StringUtils.removeFirst("abc", ".?")  = "bc"
+     * StringUtils.removeFirst("A<__>\n<__>B", "<.*>")      = "A\n<__>B"
+     * StringUtils.removeFirst("A<__>\n<__>B", "(?s)<.*>")  = "AB"
+     * StringUtils.removeFirst("ABCabc123", "[a-z]")          = "ABCbc123"
+     * StringUtils.removeFirst("ABCabc123abc", "[a-z]+")      = "ABC123abc"
+     * </pre>
+     *
+     * @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 the first replacement processed,
+     *              {@code null} if null String input
+     *
+     * @throws  java.util.regex.PatternSyntaxException
+     *              if the regular expression's syntax is invalid
+     *
+     * @see #replaceFirst(String, String, String)
+     * @see String#replaceFirst(String, String)
+     * @see java.util.regex.Pattern
+     * @see java.util.regex.Pattern#DOTALL
+     * @since 3.5
+     */
+    public static String removeFirst(final String text, final String regex) {
+        return replaceFirst(text, regex, StringUtils.EMPTY);
+    }
+
     // Replacing
     //-----------------------------------------------------------------------
     /**

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/b4842f55/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index 186da6d..2a4b415 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -2382,6 +2382,51 @@ public class StringUtilsTest {
     }
 
     @Test
+    public void testRemoveAll() {
+        assertNull(StringUtils.removeAll(null, ""));
+        assertEquals("any", StringUtils.removeAll("any", null));
+
+        assertEquals("any", StringUtils.removeAll("any", ""));
+        assertEquals("", StringUtils.removeAll("any", ".*"));
+        assertEquals("", StringUtils.removeAll("any", ".+"));
+        assertEquals("", StringUtils.removeAll("any", ".?"));
+
+        assertEquals("A\nB", StringUtils.removeAll("A<__>\n<__>B", "<.*>"));
+        assertEquals("AB", StringUtils.removeAll("A<__>\n<__>B", "(?s)<.*>"));
+        assertEquals("ABC123", StringUtils.removeAll("ABCabc123abc", "[a-z]"));
+
+        try {
+            StringUtils.removeAll("any", "{badRegexSyntax}");
+            fail("StringUtils.removeAll expecting PatternSyntaxException");
+        } catch (final PatternSyntaxException ex) {
+            // empty
+        }
+    }
+
+    @Test
+    public void testRemoveFirst() {
+        assertNull(StringUtils.removeFirst(null, ""));
+        assertEquals("any", StringUtils.removeFirst("any", null));
+
+        assertEquals("any", StringUtils.removeFirst("any", ""));
+        assertEquals("", StringUtils.removeFirst("any", ".*"));
+        assertEquals("", StringUtils.removeFirst("any", ".+"));
+        assertEquals("bc", StringUtils.removeFirst("abc", ".?"));
+
+        assertEquals("A\n<__>B", StringUtils.removeFirst("A<__>\n<__>B", "<.*>"));
+        assertEquals("AB", StringUtils.removeFirst("A<__>\n<__>B", "(?s)<.*>"));
+        assertEquals("ABCbc123", StringUtils.removeFirst("ABCabc123", "[a-z]"));
+        assertEquals("ABC123abc", StringUtils.removeFirst("ABCabc123abc", "[a-z]+"));
+
+        try {
+            StringUtils.removeFirst("any", "{badRegexSyntax}");
+            fail("StringUtils.removeFirst expecting PatternSyntaxException");
+        } catch (final PatternSyntaxException ex) {
+            // empty
+        }
+    }
+
+    @Test
     public void testDifferenceAt_StringArray() {
         assertEquals(-1, StringUtils.indexOfDifference((String[]) null));
         assertEquals(-1, StringUtils.indexOfDifference(new String[]{}));


[2/3] [lang] LANG-1185 Add some tests for StringUtils replaceAll and replaceFirst methods

Posted by lg...@apache.org.
LANG-1185 Add some tests for StringUtils replaceAll and replaceFirst 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/242b8154
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/242b8154
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/242b8154

Branch: refs/heads/master
Commit: 242b815437b9a988c2ce9e980295ca8a9e302e7a
Parents: b4842f5
Author: Loic Guibert <lg...@apache.org>
Authored: Thu Nov 5 17:17:20 2015 +0400
Committer: Loic Guibert <lg...@apache.org>
Committed: Thu Nov 5 17:17:20 2015 +0400

----------------------------------------------------------------------
 src/main/java/org/apache/commons/lang3/StringUtils.java     | 2 ++
 src/test/java/org/apache/commons/lang3/StringUtilsTest.java | 6 ++++--
 2 files changed, 6 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/242b8154/src/main/java/org/apache/commons/lang3/StringUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index d83f28b..a76e3f2 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -4867,6 +4867,7 @@ public class StringUtils {
      * StringUtils.replaceAll("", "", "zzz")    = "zzz"
      * StringUtils.replaceAll("", ".*", "zzz")  = "zzz"
      * StringUtils.replaceAll("", ".+", "zzz")  = ""
+     * StringUtils.replaceAll("abc", "", "ZZ")  = "ZZaZZbZZcZZ"
      * StringUtils.replaceAll("<__>\n<__>", "<.*>", "z")      = "z\nz"
      * StringUtils.replaceAll("<__>\n<__>", "(?s)<.*>", "z")  = "z"
      * StringUtils.replaceAll("ABCabc123", "[a-z]", "_")       = "ABC___123"
@@ -4920,6 +4921,7 @@ public class StringUtils {
      * StringUtils.replaceFirst("", "", "zzz")    = "zzz"
      * StringUtils.replaceFirst("", ".*", "zzz")  = "zzz"
      * StringUtils.replaceFirst("", ".+", "zzz")  = ""
+     * StringUtils.replaceFirst("abc", "", "ZZ")  = "ZZabc"
      * StringUtils.replaceFirst("<__>\n<__>", "<.*>", "z")      = "z\n<__>"
      * StringUtils.replaceFirst("<__>\n<__>", "(?s)<.*>", "z")  = "z"
      * StringUtils.replaceFirst("ABCabc123", "[a-z]", "_")          = "ABC_bc123"

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/242b8154/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index 2a4b415..d6fc7ae 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -1226,7 +1226,7 @@ public class StringUtilsTest {
     }
 
     @Test
-    public void testReplaceAll_StringStringString() {
+    public void testReplaceAll() {
         assertNull(StringUtils.replaceAll(null, "", ""));
 
         assertEquals("any", StringUtils.replaceAll("any", null, ""));
@@ -1235,6 +1235,7 @@ public class StringUtilsTest {
         assertEquals("zzz", StringUtils.replaceAll("", "", "zzz"));
         assertEquals("zzz", StringUtils.replaceAll("", ".*", "zzz"));
         assertEquals("", StringUtils.replaceAll("", ".+", "zzz"));
+        assertEquals("ZZaZZbZZcZZ", StringUtils.replaceAll("abc", "", "ZZ"));
 
         assertEquals("z\nz", StringUtils.replaceAll("<__>\n<__>", "<.*>", "z"));
         assertEquals("z", StringUtils.replaceAll("<__>\n<__>", "(?s)<.*>", "z"));
@@ -1254,7 +1255,7 @@ public class StringUtilsTest {
     }
 
     @Test
-    public void testReplaceFirst_StringStringString() {
+    public void testReplaceFirst() {
         assertNull(StringUtils.replaceFirst(null, "", ""));
 
         assertEquals("any", StringUtils.replaceFirst("any", null, ""));
@@ -1263,6 +1264,7 @@ public class StringUtilsTest {
         assertEquals("zzz", StringUtils.replaceFirst("", "", "zzz"));
         assertEquals("zzz", StringUtils.replaceFirst("", ".*", "zzz"));
         assertEquals("", StringUtils.replaceFirst("", ".+", "zzz"));
+        assertEquals("ZZabc", StringUtils.replaceFirst("abc", "", "ZZ"));
 
         assertEquals("z\n<__>", StringUtils.replaceFirst("<__>\n<__>", "<.*>", "z"));
         assertEquals("z", StringUtils.replaceFirst("<__>\n<__>", "(?s)<.*>", "z"));


[3/3] [lang] Add LANG-1185 to changes.xml

Posted by lg...@apache.org.
Add LANG-1185 to changes.xml


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/a5650a16
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/a5650a16
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/a5650a16

Branch: refs/heads/master
Commit: a5650a16717962d88f037d15aa52fe62d6943901
Parents: 242b815
Author: Loic Guibert <lg...@apache.org>
Authored: Thu Nov 5 17:18:58 2015 +0400
Committer: Loic Guibert <lg...@apache.org>
Committed: Thu Nov 5 17:18:58 2015 +0400

----------------------------------------------------------------------
 src/changes/changes.xml | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/a5650a16/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 432c77c..2dcb559 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.5" date="tba" description="tba">
+    <action issue="LANG-1185" type="add" dev="lguibert">Add remove by regular expression methods in StringUtils</action>
     <action issue="LANG-1183" type="update" dev="lguibert">Making replacePattern/removePattern methods null safe in StringUtils</action>
     <action issue="LANG-1139" type="add" dev="lguibert">Add replace by regular expression methods in StringUtils</action>
     <action issue="LANG-1171" type="add" dev="lguibert">Add compare methods in StringUtils</action>