diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 7bf62d11d..d83f28bd3 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); } + /** + *
Removes each substring of the text String that matches the given regular expression.
+ * + * This method is a {@code null} safe equivalent to: + *A {@code null} reference passed to this method is a no-op.
+ * + *Unlike in the {@link #removePattern(String, String)} method, the {@link Pattern#DOTALL} option
+ * is NOT automatically added.
+ * To use the DOTALL option prepend "(?s)"
to the regex.
+ * DOTALL is also know as single-line mode in Perl.
+ * 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" + *+ * + * @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); + } + + /** + *
Removes the first substring of the text string that matches the given regular expression.
+ * + * This method is a {@code null} safe equivalent to: + *A {@code null} reference passed to this method is a no-op.
+ * + *The {@link Pattern#DOTALL} option is NOT automatically added.
+ * To use the DOTALL option prepend "(?s)"
to the regex.
+ * DOTALL is also know as single-line mode in Perl.
+ * 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" + *+ * + * @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 //----------------------------------------------------------------------- /** diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index 186da6d0c..2a4b41549 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -2381,6 +2381,51 @@ public class StringUtilsTest { assertEquals("queued", StringUtils.remove("queued", 'z')); } + @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));