You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/10/10 00:07:09 UTC

svn commit: r1396375 - in /commons/proper/lang/trunk/src: changes/changes.xml main/java/org/apache/commons/lang3/StringUtils.java test/java/org/apache/commons/lang3/StringUtilsTest.java

Author: ggregory
Date: Tue Oct  9 22:07:08 2012
New Revision: 1396375

URL: http://svn.apache.org/viewvc?rev=1396375&view=rev
Log:
[LANG-841] Add StringUtils API to call String.replaceAll in DOTALL a.k.a. single-line mode.

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1396375&r1=1396374&r2=1396375&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml Tue Oct  9 22:07:08 2012
@@ -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>

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java?rev=1396375&r1=1396374&r2=1396375&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java Tue Oct  9 22:07:08 2012
@@ -3699,6 +3699,46 @@ public class StringUtils {
     }
 
     /**
+     * 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>
      *
      * <p>A {@code null} reference passed to this method is a no-op.</p>

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java?rev=1396375&r1=1396374&r2=1396375&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java Tue Oct  9 22:07:08 2012
@@ -988,6 +988,16 @@ public class StringUtilsTest {
     }
     
     @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));
         assertEquals(null, StringUtils.replace(null, null, "any", 2));