Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-841

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

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 3.2
    • lang.*
    • None

    Description

      I often want to call:

      "OldValue\nOldValue".replaceAll("Regex", "NewValue")
      

      where the old value has end of line chars. So I need to do this:

      Pattern.compile("Regex", Pattern.DOTALL).matcher("OldValue\nOldValue").replaceAll("NewValue");
      

      which I put in a util methods, which feels like it could be in 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 replaceAllPatternDotAll(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 removeAllPatternDotAll(String source, String regex) {
              return replaceAllPatternDotAll(source, regex, StringUtils.EMPTY);
          }
      

      Patch attached.

      Feedback? Better method names?

      Attachments

        1. lang-841-v3.diff
          3 kB
          Gary D. Gregory

        Activity

          People

            Unassigned Unassigned
            ggregory Gary D. Gregory
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: