You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gary D. Gregory (JIRA)" <ji...@apache.org> on 2012/10/05 16:04:03 UTC
[jira] [Commented] (LANG-841) Add StringUtils API to call
String.replaceAll in DOTALL a.k.a. single-line mode
[ https://issues.apache.org/jira/browse/LANG-841?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13470335#comment-13470335 ]
Gary D. Gregory commented on LANG-841:
--------------------------------------
I updated the ticket title and description to be much more detailed. The v2 patch reflects the changes.
I am still looking for better API names.
> 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("(?s)" + 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