Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strings.replaceAll(target, regex, fn(MatchResult, String)) #651

Open
gissuebot opened this issue Oct 31, 2014 · 11 comments
Open

Strings.replaceAll(target, regex, fn(MatchResult, String)) #651

gissuebot opened this issue Oct 31, 2014 · 11 comments

Comments

@gissuebot
Copy link

Original issue created by pholser on 2011-07-07 at 01:52 AM


A possible implementation:

public class Strings {
    private Strings() {
        throw new UnsupportedOperationException();
    }

public static String replaceAll(String target, String regex, Function<MatchResult, String> operation) {
    StringBuffer result = new StringBuffer(target.length() * 3 / 2);
    Pattern pattern = Pattern.compile(regex);

    Matcher matcher = pattern.matcher(target);
    while (matcher.find()) {
        MatchResult match = matcher.toMatchResult();
        String replacement = operation.apply(match);
        if (!replacement.equals(match.group()))
            matcher.appendReplacement(result, replacement);
    }
    matcher.appendTail(result);

    return result.toString();
}

}

I'm ambivalent about whether to make the type of 'regex' a Pattern or leave it as a String for the method to compile().

Motivation: To allow global replacement of matches with the result of a function of the match result, a la C#'s Regex.Replace(string, MatchEvaluator). PITA to roll the appendReplacement/appendTail bit by hand.

@gissuebot
Copy link
Author

Original comment posted by [email protected] on 2011-07-13 at 06:32 PM


Something almost exactly like this exists in the internal Google codebase; it was one of the first libraries I contributed ~5 years ago and almost no one has ever used it. :)


Status: Triaged

@gissuebot
Copy link
Author

Original comment posted by [email protected] on 2011-07-13 at 07:00 PM


(No comment entered for this change.)


Labels: Type-Enhancement

@gissuebot
Copy link
Author

Original comment posted by [email protected] on 2011-07-13 at 07:42 PM


(No comment entered for this change.)

@gissuebot
Copy link
Author

Original comment posted by [email protected] on 2011-12-10 at 04:12 PM


(No comment entered for this change.)


Labels: Package-Base

@gissuebot
Copy link
Author

Original comment posted by [email protected] on 2012-02-16 at 07:17 PM


Possibly related: bug 383

@gissuebot
Copy link
Author

Original comment posted by [email protected] on 2012-02-16 at 07:17 PM


(No comment entered for this change.)


Status: Acknowledged

@gissuebot
Copy link
Author

Original comment posted by wasserman.louis on 2012-02-16 at 07:19 PM


Issue #383 has been merged into this issue.

@gissuebot
Copy link
Author

Original comment posted by [email protected] on 2012-02-16 at 07:20 PM


I think something might need to be done in this area but we need to mount a small research project to understand exactly what, and that is probably not high-priority at the moment.

@gissuebot
Copy link
Author

Original comment posted by [email protected] on 2012-05-30 at 07:43 PM


(No comment entered for this change.)


Labels: -Type-Enhancement, Type-Addition

@gissuebot
Copy link
Author

Original comment posted by [email protected] on 2012-06-22 at 06:16 PM


(No comment entered for this change.)


Status: Research

@ogregoire
Copy link

ogregoire commented Jul 12, 2016

I've started an implementation that matches exactly, not based on regex, but I guess it can easily be improved.

It can be seen here: https://gist.github.com/ogregoire/8c95bd3ab7c7e670a5012caad47125e3

If I'm not lost in other considerations, I'll try to make this into a guava-like class and create a PR. Any remarks, comments is welcome.

@netdpb netdpb added the P3 no SLO label Jul 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants