commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gary D. Gregory (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (LANG-841) Add StringUtils API to call String.replaceAll in DOTALL a.k.a. single-line mode
Date Mon, 08 Oct 2012 15:16:03 GMT

    [ https://issues.apache.org/jira/browse/LANG-841?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13471609#comment-13471609
] 

Gary D. Gregory commented on LANG-841:
--------------------------------------

Hm, "All" and "Entire" next to each other look weird, especially because they are synonymous.

I used the word Pattern to differentiate the method from the other replace methods that do
NOT take a Regex:

- replace(String, String, String)
- replace(String, String, String, int)
- replaceChars(String, char, char)
- replaceChars(String, String, String)
- replaceEach(String, String[], String[])
- replaceEach(String, String[], String[], boolean, int)
- replaceEachRepeatedly(String, String[], String[])
- replaceOnce(String, String, String)

There should be something in the name that suggest the method works with a Regex.

I also used the word Pattern because the underlying Java class is Pattern. Using "Regex" is
also possible but not better than Pattern IMO.

Maybe we should flip it where we assume that the API searches the whole String, like the other
APIs, the only difference is that a Regex is used for matching. If you don't want to search
the whole String, then you do not need this API! 

So we already have:

replace(String, String, String) for non-regex

replacePattern(String, String, String) for using regex and the Pattern class!

I like it much better!

                
> Add StringUtils API to call String.replaceAll in DOTALL a.k.a. single-line mode
> -------------------------------------------------------------------------------
>
>                 Key: LANG-841
>                 URL: https://issues.apache.org/jira/browse/LANG-841
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.*
>         Environment: Apache Maven 3.0.4 (r1232337; 2012-01-17 03:44:56-0500)
> Maven home: C:\Java\apache-maven-3.0.4\bin\..
> Java version: 1.6.0_35, vendor: Sun Microsystems Inc.
> Java home: C:\Program Files\Java\jdk1.6.0_35\jre
> Default locale: en_US, platform encoding: Cp1252
> OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
>            Reporter: Gary D. Gregory
>         Attachments: lang-841.diff
>
>
> I often want to call:
> {code:java}
> "OldValue\nOldValue".replaceAll("Regex", "NewValue")
> {code}
> where the old value has end of line chars. So I need to do this:
> {code:java}
> Pattern.compile("Regex", Pattern.DOTALL).matcher("OldValue\nOldValue").replaceAll("NewValue");
> {code}
> which I put in a util methods, which feels like it could be in StringUtils:
> {code:java}
>     /**
>      * 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);
>     }
> {code}
> Patch attached. 
> Feedback? Better method names?

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message