yangxu998 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

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

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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.

Original issue reported on code.google.com by phol...@gmail.com on 7 Jul 2011 at 1:52

GoogleCodeExporter commented 9 years ago
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. :)

Original comment by kevinb@google.com on 13 Jul 2011 at 6:32

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 13 Jul 2011 at 7:00

GoogleCodeExporter commented 9 years ago

Original comment by cpov...@google.com on 13 Jul 2011 at 7:42

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 10 Dec 2011 at 4:12

GoogleCodeExporter commented 9 years ago
Possibly related:  bug 383

Original comment by cpov...@google.com on 16 Feb 2012 at 7:17

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 16 Feb 2012 at 7:17

GoogleCodeExporter commented 9 years ago
Issue 383 has been merged into this issue.

Original comment by wasserman.louis on 16 Feb 2012 at 7:19

GoogleCodeExporter commented 9 years ago
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.

Original comment by kevinb@google.com on 16 Feb 2012 at 7:20

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 30 May 2012 at 7:43

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 22 Jun 2012 at 6:16

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:18

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09