hg: lambda/lambda/jdk: Pattern.splitAsStream.
Paul Sandoz
paul.sandoz at oracle.com
Mon Apr 15 05:32:54 PDT 2013
Hi Jürgen,
That seems useful as a more general approach than Matcher.replaceAll(String ) e.g.
Matcher.replaceAll(Function<MatchResult, String> f)
Ben, thoughts?
Paul.
On Apr 8, 2013, at 6:59 PM, jk at blackdown.de wrote:
> Hi Paul,
>
> it would be nice if Pattern/Matcher offered a terse way to loop over all
> matches in a string and replace them via a callback.
>
> E.g. I'm currently using something like this:
>
> private static final PatternAndReplacement PASS2 = new PatternAndReplacement(
> Pattern.compile(" ( "
> + " \\A \\p{Punct}*" // start of title…
> + " |"
> + " [:.;?!]\\ +" // or of subsentence…
> + " | "
> + " \\ ['\"“‘(\\[] \\ *" // or of inserted subphrase…
> + ")"
> + "(" + SMALL_WORDS + ") \\b", // … followed by small word
> Pattern.COMMENTS | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS),
> m -> Matcher.quoteReplacement(m.group(1) + capitalize(m.group(2))));
>
> with PatternAndReplacement being
>
> private static class PatternReplacement implements Function<String, String> {
> private final Pattern pattern;
> private final Function<MatchResult, String> function;
>
> PatternReplacement(final Pattern p, final Function<MatchResult, String> f) {
> pattern = p;
> function = f;
> }
>
> @Override
> public final String apply(final String s) {
> Matcher m = pattern.matcher(s);
> if (m.find()) {
> StringBuffer sb = new StringBuffer(s.length());
> do {
> m.appendReplacement(sb, function.apply(m));
> } while (m.find());
> return m.appendTail(sb).toString();
> }
> return s;
> }
> }
>
> Any plans for something like this?
>
>
> Jürgen
>
>
> paul.sandoz at oracle.com writes:
>
>> Changeset: 526131346981
>> Author: psandoz
>> Date: 2013-04-08 17:16 +0200
>> URL: http://hg.openjdk.java.net/lambda/lambda/jdk/rev/526131346981
>>
>> Pattern.splitAsStream.
>> Contributed-by: Ben Evans <benjamin.john.evans at gmail.com>
>>
>> ! src/share/classes/java/util/regex/Pattern.java
>> + test-ng/tests/org/openjdk/tests/java/util/regex/PatternTest.java
More information about the lambda-dev
mailing list