diff --git a/src/main/java/org/apache/commons/lang3/RegExUtils.java b/src/main/java/org/apache/commons/lang3/RegExUtils.java index 97c32fc2ce8..d1481501a7d 100644 --- a/src/main/java/org/apache/commons/lang3/RegExUtils.java +++ b/src/main/java/org/apache/commons/lang3/RegExUtils.java @@ -25,6 +25,42 @@ */ 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: + *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.
* @@ -72,39 +108,39 @@ public static String removeAll(final String text, final String regex) { } /** - *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: *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(".*")) = "" + * StringUtils.removeFirst("any", Pattern.compile(".+")) = "" + * StringUtils.removeFirst("abc", Pattern.compile(".?")) = "bc" + * StringUtils.removeFirst("A<__>\n<__>B", Pattern.compile("<.*>")) = "A\n<__>B" + * StringUtils.removeFirst("A<__>\n<__>B", Pattern.compile("(?s)<.*>")) = "AB" + * StringUtils.removeFirst("ABCabc123", Pattern.compile("[a-z]")) = "ABCbc123" + * StringUtils.removeFirst("ABCabc123abc", Pattern.compile("[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 any removes processed, + * @param regex the regular expression pattern to which this string is to be matched + * @return the text with the first replacement processed, * {@code null} if null String input * - * @see #replaceAll(String, Pattern, String) - * @see java.util.regex.Matcher#replaceAll(String) + * @see #replaceFirst(String, Pattern, String) + * @see java.util.regex.Matcher#replaceFirst(String) * @see java.util.regex.Pattern */ - public static String removeAll(final String text, final Pattern regex) { - return replaceAll(text, regex, StringUtils.EMPTY); + public static String removeFirst(final String text, final Pattern regex) { + return replaceFirst(text, regex, StringUtils.EMPTY); } /** @@ -153,114 +189,77 @@ public static String removeFirst(final String text, final String regex) { } /** - *
Removes the first substring of the text string that matches the given regular expression pattern.
- * - * This method is a {@code null} safe equivalent to: - *A {@code null} reference passed to this method is a no-op.
- * - *- * StringUtils.removeFirst(null, *) = null - * StringUtils.removeFirst("any", (Pattern) null) = "any" - * StringUtils.removeFirst("any", Pattern.compile("")) = "any" - * StringUtils.removeFirst("any", Pattern.compile(".*")) = "" - * StringUtils.removeFirst("any", Pattern.compile(".+")) = "" - * StringUtils.removeFirst("abc", Pattern.compile(".?")) = "bc" - * StringUtils.removeFirst("A<__>\n<__>B", Pattern.compile("<.*>")) = "A\n<__>B" - * StringUtils.removeFirst("A<__>\n<__>B", Pattern.compile("(?s)<.*>")) = "AB" - * StringUtils.removeFirst("ABCabc123", Pattern.compile("[a-z]")) = "ABCbc123" - * StringUtils.removeFirst("ABCabc123abc", Pattern.compile("[a-z]+")) = "ABC123abc" - *- * - * @param text text to remove from, may be null - * @param regex the regular expression pattern to which this string is to be matched - * @return the text with the first replacement processed, - * {@code null} if null String input - * - * @see #replaceFirst(String, Pattern, String) - * @see java.util.regex.Matcher#replaceFirst(String) - * @see java.util.regex.Pattern - */ - public static String removeFirst(final String text, final Pattern regex) { - return replaceFirst(text, regex, StringUtils.EMPTY); - } - - /** - *
Replaces each substring of the source String that matches the given regular expression with the given - * replacement using the {@link Pattern#DOTALL} option. DOTALL is also known as single-line mode in Perl.
+ *Removes each substring of the source String that matches the given regular expression using the DOTALL option.
* * This call is a {@code null} safe equivalent to: *A {@code null} reference passed to this method is a no-op.
* *- * StringUtils.replacePattern(null, *, *) = null - * StringUtils.replacePattern("any", (String) null, *) = "any" - * StringUtils.replacePattern("any", *, null) = "any" - * StringUtils.replacePattern("", "", "zzz") = "zzz" - * StringUtils.replacePattern("", ".*", "zzz") = "zzz" - * StringUtils.replacePattern("", ".+", "zzz") = "" - * StringUtils.replacePattern("<__>\n<__>", "<.*>", "z") = "z" - * StringUtils.replacePattern("ABCabc123", "[a-z]", "_") = "ABC___123" - * StringUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "_") = "ABC_123" - * StringUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "") = "ABC123" - * StringUtils.replacePattern("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2") = "Lorem_ipsum_dolor_sit" + * StringUtils.removePattern(null, *) = null + * StringUtils.removePattern("any", (String) null) = "any" + * StringUtils.removePattern("A<__>\n<__>B", "<.*>") = "AB" + * StringUtils.removePattern("ABCabc123", "[a-z]") = "ABC123" ** * @param text * the source string * @param regex * the regular expression to which this string is to be matched - * @param replacement - * the string to be substituted for each match * @return The resulting {@code String} - * @see #replaceAll(String, String, String) + * @see #replacePattern(String, String, String) * @see String#replaceAll(String, String) * @see Pattern#DOTALL */ - public static String replacePattern(final String text, final String regex, final String replacement) { - if (text == null || regex == null || replacement == null) { - return text; - } - return Pattern.compile(regex, Pattern.DOTALL).matcher(text).replaceAll(replacement); + public static String removePattern(final String text, final String regex) { + return replacePattern(text, regex, StringUtils.EMPTY); } /** - *
Removes each substring of the source String that matches the given regular expression using the DOTALL option.
+ *Replaces each substring of the text String that matches the given regular expression pattern with the given replacement.
* - * This call is a {@code null} safe equivalent to: + * This method is a {@code null} safe equivalent to: *A {@code null} reference passed to this method is a no-op.
* *- * StringUtils.removePattern(null, *) = null - * StringUtils.removePattern("any", (String) null) = "any" - * StringUtils.removePattern("A<__>\n<__>B", "<.*>") = "AB" - * StringUtils.removePattern("ABCabc123", "[a-z]") = "ABC123" + * StringUtils.replaceAll(null, *, *) = null + * StringUtils.replaceAll("any", (Pattern) null, *) = "any" + * StringUtils.replaceAll("any", *, null) = "any" + * StringUtils.replaceAll("", Pattern.compile(""), "zzz") = "zzz" + * StringUtils.replaceAll("", Pattern.compile(".*"), "zzz") = "zzz" + * StringUtils.replaceAll("", Pattern.compile(".+"), "zzz") = "" + * StringUtils.replaceAll("abc", Pattern.compile(""), "ZZ") = "ZZaZZbZZcZZ" + * StringUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>"), "z") = "z\nz" + * StringUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>", Pattern.DOTALL), "z") = "z" + * StringUtils.replaceAll("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z") = "z" + * StringUtils.replaceAll("ABCabc123", Pattern.compile("[a-z]"), "_") = "ABC___123" + * StringUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "_") = "ABC_123" + * StringUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "") = "ABC123" + * StringUtils.replaceAll("Lorem ipsum dolor sit", Pattern.compile("( +)([a-z]+)"), "_$2") = "Lorem_ipsum_dolor_sit" ** - * @param text - * the source string - * @param regex - * the regular expression to which this string is to be matched - * @return The resulting {@code String} - * @see #replacePattern(String, String, String) - * @see String#replaceAll(String, String) - * @see Pattern#DOTALL + * @param text text to search and replace in, may be null + * @param regex the regular expression pattern to which this string is to be matched + * @param replacement the string to be substituted for each match + * @return the text with any replacements processed, + * {@code null} if null String input + * + * @see java.util.regex.Matcher#replaceAll(String) + * @see java.util.regex.Pattern */ - public static String removePattern(final String text, final String regex) { - return replacePattern(text, regex, StringUtils.EMPTY); + public static String replaceAll(final String text, final Pattern regex, final String replacement) { + if (text == null || regex == null || replacement == null) { + return text; + } + return regex.matcher(text).replaceAll(replacement); } /** @@ -318,46 +317,46 @@ public static String replaceAll(final String text, final String regex, final Str } /** - *
Replaces each substring of the text String that matches the given regular expression pattern with the given replacement.
+ *Replaces the first substring of the text string that matches the given regular expression pattern + * with the given replacement.
* * This method is a {@code null} safe equivalent to: *A {@code null} reference passed to this method is a no-op.
* *- * StringUtils.replaceAll(null, *, *) = null - * StringUtils.replaceAll("any", (Pattern) null, *) = "any" - * StringUtils.replaceAll("any", *, null) = "any" - * StringUtils.replaceAll("", Pattern.compile(""), "zzz") = "zzz" - * StringUtils.replaceAll("", Pattern.compile(".*"), "zzz") = "zzz" - * StringUtils.replaceAll("", Pattern.compile(".+"), "zzz") = "" - * StringUtils.replaceAll("abc", Pattern.compile(""), "ZZ") = "ZZaZZbZZcZZ" - * StringUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>"), "z") = "z\nz" - * StringUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>", Pattern.DOTALL), "z") = "z" - * StringUtils.replaceAll("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z") = "z" - * StringUtils.replaceAll("ABCabc123", Pattern.compile("[a-z]"), "_") = "ABC___123" - * StringUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "_") = "ABC_123" - * StringUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "") = "ABC123" - * StringUtils.replaceAll("Lorem ipsum dolor sit", Pattern.compile("( +)([a-z]+)"), "_$2") = "Lorem_ipsum_dolor_sit" + * StringUtils.replaceFirst(null, *, *) = null + * StringUtils.replaceFirst("any", (Pattern) null, *) = "any" + * StringUtils.replaceFirst("any", *, null) = "any" + * StringUtils.replaceFirst("", Pattern.compile(""), "zzz") = "zzz" + * StringUtils.replaceFirst("", Pattern.compile(".*"), "zzz") = "zzz" + * StringUtils.replaceFirst("", Pattern.compile(".+"), "zzz") = "" + * StringUtils.replaceFirst("abc", Pattern.compile(""), "ZZ") = "ZZabc" + * StringUtils.replaceFirst("<__>\n<__>", Pattern.compile("<.*>"), "z") = "z\n<__>" + * StringUtils.replaceFirst("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z") = "z" + * StringUtils.replaceFirst("ABCabc123", Pattern.compile("[a-z]"), "_") = "ABC_bc123" + * StringUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), "_") = "ABC_123abc" + * StringUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), "") = "ABC123abc" + * StringUtils.replaceFirst("Lorem ipsum dolor sit", Pattern.compile("( +)([a-z]+)"), "_$2") = "Lorem_ipsum dolor sit" ** * @param text text to search and replace in, may be null * @param regex the regular expression pattern to which this string is to be matched - * @param replacement the string to be substituted for each match - * @return the text with any replacements processed, + * @param replacement the string to be substituted for the first match + * @return the text with the first replacement processed, * {@code null} if null String input * - * @see java.util.regex.Matcher#replaceAll(String) + * @see java.util.regex.Matcher#replaceFirst(String) * @see java.util.regex.Pattern */ - public static String replaceAll(final String text, final Pattern regex, final String replacement) { - if (text == null || regex == null || replacement == null) { + public static String replaceFirst(final String text, final Pattern regex, final String replacement) { + if (text == null || regex == null|| replacement == null ) { return text; } - return regex.matcher(text).replaceAll(replacement); + return regex.matcher(text).replaceFirst(replacement); } /** @@ -413,46 +412,47 @@ public static String replaceFirst(final String text, final String regex, final S } /** - *
Replaces the first substring of the text string that matches the given regular expression pattern - * with the given replacement.
+ *Replaces each substring of the source String that matches the given regular expression with the given + * replacement using the {@link Pattern#DOTALL} option. DOTALL is also known as single-line mode in Perl.
* - * This method is a {@code null} safe equivalent to: + * This call is a {@code null} safe equivalent to: *A {@code null} reference passed to this method is a no-op.
* *- * StringUtils.replaceFirst(null, *, *) = null - * StringUtils.replaceFirst("any", (Pattern) null, *) = "any" - * StringUtils.replaceFirst("any", *, null) = "any" - * StringUtils.replaceFirst("", Pattern.compile(""), "zzz") = "zzz" - * StringUtils.replaceFirst("", Pattern.compile(".*"), "zzz") = "zzz" - * StringUtils.replaceFirst("", Pattern.compile(".+"), "zzz") = "" - * StringUtils.replaceFirst("abc", Pattern.compile(""), "ZZ") = "ZZabc" - * StringUtils.replaceFirst("<__>\n<__>", Pattern.compile("<.*>"), "z") = "z\n<__>" - * StringUtils.replaceFirst("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z") = "z" - * StringUtils.replaceFirst("ABCabc123", Pattern.compile("[a-z]"), "_") = "ABC_bc123" - * StringUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), "_") = "ABC_123abc" - * StringUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), "") = "ABC123abc" - * StringUtils.replaceFirst("Lorem ipsum dolor sit", Pattern.compile("( +)([a-z]+)"), "_$2") = "Lorem_ipsum dolor sit" + * StringUtils.replacePattern(null, *, *) = null + * StringUtils.replacePattern("any", (String) null, *) = "any" + * StringUtils.replacePattern("any", *, null) = "any" + * StringUtils.replacePattern("", "", "zzz") = "zzz" + * StringUtils.replacePattern("", ".*", "zzz") = "zzz" + * StringUtils.replacePattern("", ".+", "zzz") = "" + * StringUtils.replacePattern("<__>\n<__>", "<.*>", "z") = "z" + * StringUtils.replacePattern("ABCabc123", "[a-z]", "_") = "ABC___123" + * StringUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "_") = "ABC_123" + * StringUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "") = "ABC123" + * StringUtils.replacePattern("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2") = "Lorem_ipsum_dolor_sit" ** - * @param text text to search and replace in, may be null - * @param regex the regular expression pattern to which this string is to be matched - * @param replacement the string to be substituted for the first match - * @return the text with the first replacement processed, - * {@code null} if null String input - * - * @see java.util.regex.Matcher#replaceFirst(String) - * @see java.util.regex.Pattern + * @param text + * the source string + * @param regex + * the regular expression to which this string is to be matched + * @param replacement + * the string to be substituted for each match + * @return The resulting {@code String} + * @see #replaceAll(String, String, String) + * @see String#replaceAll(String, String) + * @see Pattern#DOTALL */ - public static String replaceFirst(final String text, final Pattern regex, final String replacement) { - if (text == null || regex == null|| replacement == null ) { + public static String replacePattern(final String text, final String regex, final String replacement) { + if (text == null || regex == null || replacement == null) { return text; } - return regex.matcher(text).replaceFirst(replacement); + return Pattern.compile(regex, Pattern.DOTALL).matcher(text).replaceAll(replacement); } }