Merge branch 'fix-LANG-1139'
LANG-1139: Add replace by regular expression methods in StringUtils This closes #92 from github.
This commit is contained in:
commit
a6addf94e5
|
@ -22,6 +22,7 @@
|
|||
<body>
|
||||
|
||||
<release version="3.5" date="tba" description="tba">
|
||||
<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>
|
||||
<action issue="LANG-1174" type="add" dev="britter" due-to="Punkratz312">Add sugar to RandomUtils</action>
|
||||
<action issue="LANG-1057" type="update" dev="chas" due-to="Otávio Santana">Replace StringBuilder with String concatenation for better optimization</action>
|
||||
|
|
|
@ -25,6 +25,7 @@ import java.util.Iterator;
|
|||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
/**
|
||||
* <p>Operations on {@link java.lang.String} that are
|
||||
|
@ -4710,6 +4711,111 @@ public class StringUtils {
|
|||
return replacePattern(source, regex, StringUtils.EMPTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Replaces each substring of the text String that matches the given regular expression
|
||||
* with the given replacement.</p>
|
||||
*
|
||||
* This method is a {@code null} safe equivalent to:
|
||||
* <ul>
|
||||
* <li>{@code text.replaceAll(regex, replacement)}</li>
|
||||
* <li>{@code Pattern.compile(regex).matcher(text).replaceAll(replacement)}</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>A {@code null} reference passed to this method is a no-op.</p>
|
||||
*
|
||||
* <p>Unlike in the {@link #replacePattern(String, 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.replaceAll(null, *, *) = null
|
||||
* StringUtils.replaceAll("any", null, *) = "any"
|
||||
* StringUtils.replaceAll("any", *, null) = "any"
|
||||
* StringUtils.replaceAll("", "", "zzz") = "zzz"
|
||||
* StringUtils.replaceAll("", ".*", "zzz") = "zzz"
|
||||
* StringUtils.replaceAll("", ".+", "zzz") = ""
|
||||
* StringUtils.replaceAll("<__>\n<__>", "<.*>", "z") = "z\nz"
|
||||
* StringUtils.replaceAll("<__>\n<__>", "(?s)<.*>", "z") = "z"
|
||||
* StringUtils.replaceAll("ABCabc123", "[a-z]", "_") = "ABC___123"
|
||||
* StringUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "_") = "ABC_123"
|
||||
* StringUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "") = "ABC123"
|
||||
* StringUtils.replaceAll("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2") = "Lorem_ipsum_dolor_sit"
|
||||
* </pre>
|
||||
*
|
||||
* @param text text to search and replace in, may be null
|
||||
* @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 text with any replacements processed,
|
||||
* {@code null} if null String input
|
||||
*
|
||||
* @throws PatternSyntaxException
|
||||
* if the regular expression's syntax is invalid
|
||||
*
|
||||
* @see String#replaceAll(String, String)
|
||||
* @see java.util.regex.Pattern
|
||||
* @see java.util.regex.Pattern#DOTALL
|
||||
* @since 3.5
|
||||
*/
|
||||
public static String replaceAll(final String text, final String regex, final String replacement) {
|
||||
if (text == null || regex == null|| replacement == null ) {
|
||||
return text;
|
||||
}
|
||||
return text.replaceAll(regex, replacement);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Replaces the first substring of the text string that matches the given regular expression
|
||||
* with the given replacement.</p>
|
||||
*
|
||||
* This method is a {@code null} safe equivalent to:
|
||||
* <ul>
|
||||
* <li>{@code text.replaceFirst(regex, replacement)}</li>
|
||||
* <li>{@code Pattern.compile(regex).matcher(text).replaceFirst(replacement)}</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.replaceFirst(null, *, *) = null
|
||||
* StringUtils.replaceFirst("any", null, *) = "any"
|
||||
* StringUtils.replaceFirst("any", *, null) = "any"
|
||||
* StringUtils.replaceFirst("", "", "zzz") = "zzz"
|
||||
* StringUtils.replaceFirst("", ".*", "zzz") = "zzz"
|
||||
* StringUtils.replaceFirst("", ".+", "zzz") = ""
|
||||
* StringUtils.replaceFirst("<__>\n<__>", "<.*>", "z") = "z\n<__>"
|
||||
* StringUtils.replaceFirst("<__>\n<__>", "(?s)<.*>", "z") = "z"
|
||||
* StringUtils.replaceFirst("ABCabc123", "[a-z]", "_") = "ABC_bc123"
|
||||
* StringUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "_") = "ABC_123abc"
|
||||
* StringUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "") = "ABC123abc"
|
||||
* StringUtils.replaceFirst("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2") = "Lorem_ipsum dolor sit"
|
||||
* </pre>
|
||||
*
|
||||
* @param text text to search and replace in, may be null
|
||||
* @param regex the regular expression 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
|
||||
*
|
||||
* @throws PatternSyntaxException
|
||||
* if the regular expression's syntax is invalid
|
||||
*
|
||||
* @see String#replaceFirst(String, String)
|
||||
* @see java.util.regex.Pattern
|
||||
* @see java.util.regex.Pattern#DOTALL
|
||||
* @since 3.5
|
||||
*/
|
||||
public static String replaceFirst(final String text, final String regex, final String replacement) {
|
||||
if (text == null || regex == null|| replacement == null ) {
|
||||
return text;
|
||||
}
|
||||
return text.replaceFirst(regex, replacement);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Replaces all occurrences of a String within another String.</p>
|
||||
*
|
||||
|
|
|
@ -35,6 +35,7 @@ import java.util.Arrays;
|
|||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import org.apache.commons.lang3.text.WordUtils;
|
||||
import org.junit.Test;
|
||||
|
@ -1196,6 +1197,62 @@ public class StringUtilsTest {
|
|||
assertEquals("", StringUtils.removePattern("<A>x\\ny</A>", "<A>.*</A>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReplaceAll_StringStringString() {
|
||||
assertNull(StringUtils.replaceAll(null, "", ""));
|
||||
|
||||
assertEquals("any", StringUtils.replaceAll("any", null, ""));
|
||||
assertEquals("any", StringUtils.replaceAll("any", "", null));
|
||||
|
||||
assertEquals("zzz", StringUtils.replaceAll("", "", "zzz"));
|
||||
assertEquals("zzz", StringUtils.replaceAll("", ".*", "zzz"));
|
||||
assertEquals("", StringUtils.replaceAll("", ".+", "zzz"));
|
||||
|
||||
assertEquals("z\nz", StringUtils.replaceAll("<__>\n<__>", "<.*>", "z"));
|
||||
assertEquals("z", StringUtils.replaceAll("<__>\n<__>", "(?s)<.*>", "z"));
|
||||
|
||||
assertEquals("ABC___123", StringUtils.replaceAll("ABCabc123", "[a-z]", "_"));
|
||||
assertEquals("ABC_123", StringUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "_"));
|
||||
assertEquals("ABC123", StringUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", ""));
|
||||
assertEquals("Lorem_ipsum_dolor_sit",
|
||||
StringUtils.replaceAll("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2"));
|
||||
|
||||
try {
|
||||
StringUtils.replaceAll("any", "{badRegexSyntax}", "");
|
||||
fail("StringUtils.replaceAll expecting PatternSyntaxException");
|
||||
} catch (final PatternSyntaxException ex) {
|
||||
// empty
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReplaceFirst_StringStringString() {
|
||||
assertNull(StringUtils.replaceFirst(null, "", ""));
|
||||
|
||||
assertEquals("any", StringUtils.replaceFirst("any", null, ""));
|
||||
assertEquals("any", StringUtils.replaceFirst("any", "", null));
|
||||
|
||||
assertEquals("zzz", StringUtils.replaceFirst("", "", "zzz"));
|
||||
assertEquals("zzz", StringUtils.replaceFirst("", ".*", "zzz"));
|
||||
assertEquals("", StringUtils.replaceFirst("", ".+", "zzz"));
|
||||
|
||||
assertEquals("z\n<__>", StringUtils.replaceFirst("<__>\n<__>", "<.*>", "z"));
|
||||
assertEquals("z", StringUtils.replaceFirst("<__>\n<__>", "(?s)<.*>", "z"));
|
||||
|
||||
assertEquals("ABC_bc123", StringUtils.replaceFirst("ABCabc123", "[a-z]", "_"));
|
||||
assertEquals("ABC_123abc", StringUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "_"));
|
||||
assertEquals("ABC123abc", StringUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", ""));
|
||||
assertEquals("Lorem_ipsum dolor sit",
|
||||
StringUtils.replaceFirst("Lorem ipsum dolor sit", "( +)([a-z]+)", "_$2"));
|
||||
|
||||
try {
|
||||
StringUtils.replaceFirst("any", "{badRegexSyntax}", "");
|
||||
fail("StringUtils.replaceFirst expecting PatternSyntaxException");
|
||||
} catch (final PatternSyntaxException ex) {
|
||||
// empty
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReplace_StringStringStringInt() {
|
||||
assertNull(StringUtils.replace(null, null, null, 2));
|
||||
|
|
Loading…
Reference in New Issue