[LANG-841] Add StringUtils API to call String.replaceAll in DOTALL a.k.a. single-line mode.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1396375 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2012-10-09 22:07:08 +00:00
parent 22d050b781
commit f7a8fa91ea
3 changed files with 51 additions and 0 deletions

View File

@ -23,6 +23,7 @@
<release version="3.2" date="TBA" description="Next release">
<action issue="LANG-844" type="fix">Fix examples contained in javadoc of StringUtils.center methods</action>
<action issue="LANG-841" type="add">Add StringUtils API to call String.replaceAll in DOTALL a.k.a. single-line mode</action>
<action issue="LANG-839" type="update">ArrayUtils removeElements methods use unnecessary HashSet</action>
<action issue="LANG-838" type="update">ArrayUtils removeElements methods clone temporary index arrays unnecessarily</action>
<action issue="LANG-832" type="fix">FastDateParser does not handle unterminated quotes correctly</action>

View File

@ -3698,6 +3698,46 @@ public static String replaceOnce(String text, String searchString, String replac
return replace(text, searchString, replacement, 1);
}
/**
* 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 know as single-line mode in Perl. This call
* is also equivalent to:
* <ul>
* <li>{@code source.replaceAll(&quot;(?s)&quot; + regex, replacement)}</li>
* <li>{@code Pattern.compile(regex, Pattern.DOTALL).matcher(source).replaceAll(replacement)}</li>
* </ul>
*
* @param source
* 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 String#replaceAll(String, String)
* @see Pattern#DOTALL
* @since 3.2
*/
public static String replacePattern(String source, String regex, String replacement) {
return Pattern.compile(regex, Pattern.DOTALL).matcher(source).replaceAll(replacement);
}
/**
* Removes each substring of the source String that matches the given regular expression using the DOTALL option.
*
* @param source
* the source string
* @param regex
* the regular expression to which this string is to be matched
* @return The resulting {@code String}
* @see String#replaceAll(String, String)
* @see Pattern#DOTALL
* @since 3.2
*/
public static String removePattern(String source, String regex) {
return replacePattern(source, regex, StringUtils.EMPTY);
}
/**
* <p>Replaces all occurrences of a String within another String.</p>
*

View File

@ -987,6 +987,16 @@ public void testReplace_StringStringString() {
assertEquals("farfarfar", StringUtils.replace("foofoofoo", "oo", "ar"));
}
@Test
public void testReplacePattern() {
assertEquals("X", StringUtils.replacePattern("<A>\nxy\n</A>", "<A>.*</A>", "X"));
}
@Test
public void testRemovePattern() {
assertEquals("", StringUtils.removePattern("<A>x\\ny</A>", "<A>.*</A>"));
}
@Test
public void testReplace_StringStringStringInt() {
assertEquals(null, StringUtils.replace(null, null, null, 2));