Java Regex String Replace replace(String input, Pattern regex, Function converter)

Here you can find the source of replace(String input, Pattern regex, Function converter)

Description

Finds and replaces matches from the input string using the regex regular expression and the converter replacement function.

License

Open Source License

Parameter

Parameter Description
input input string.
regex regular expression that must be used in order to find matches.
converter A match -> replacement function.

Return

the resultant string

Declaration

public static String replace(String input, Pattern regex, Function<String, String> converter) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**// w  w  w. jav  a  2s .c o m
     * Finds and replaces matches from the {@code input} string using the {@code regex} regular expression and the {@code converter} replacement function.
     *
     * @param input     input string.
     * @param regex     regular expression that must be used in order to find matches.
     * @param converter A {@code match -> replacement} function.
     * @return the resultant string
     */
    public static String replace(String input, Pattern regex, Function<String, String> converter) {

        Matcher matcher = regex.matcher(input);

        StringBuilder sb = new StringBuilder();

        int lastPosition = 0;

        while (matcher.find()) {

            sb.append(input.substring(lastPosition, matcher.start()));
            sb.append(converter.apply(matcher.group()));

            lastPosition = matcher.end();

        }

        sb.append(input.substring(lastPosition));

        return sb.toString();

    }

    /**
     * Finds and replaces matches from the {@code input} string using the {@code regex} regular expression and the {@code converter} replacement function.
     *
     * @param input     input string.
     * @param regex     regular expression that must be used in order to find matches.
     * @param converter A {@code (match, backreferences) -> replacement} function. {@code backreferences} array contains {@code match} in its 0-index.
     * @return the resultant string
     */
    public static String replace(String input, Pattern regex, BiFunction<String, String[], String> converter) {

        Matcher matcher = regex.matcher(input);

        StringBuilder sb = new StringBuilder();

        int lastPosition = 0;

        while (matcher.find()) {

            sb.append(input.substring(lastPosition, matcher.start()));
            sb.append(converter.apply(matcher.group(), getMatchBackReferences(matcher)));

            lastPosition = matcher.end();

        }

        sb.append(input.substring(lastPosition));

        return sb.toString();

    }

    private static String[] getMatchBackReferences(Matcher matcher) {

        String[] backreferences = new String[matcher.groupCount() + 1];

        for (int i = 0; i < backreferences.length; ++i) {
            backreferences[i] = matcher.group(i);
        }

        return backreferences;

    }
}

Related

  1. replace(final String template, final Map map, final String prefix, final String suffix, boolean useMapForMatching)
  2. replace(Matcher m, String rv, Object value)
  3. replace(Pattern pattern, String src, Function handler)
  4. replace(String content, String name, String value)
  5. replace(String input, Pattern pattern, Function replacementGenerator)
  6. replace(String inputStr, String patternStr, String replacementStr)
  7. replace(String inString, String oldPattern, String newPattern)
  8. replace(String line, String regexp, String replacement)
  9. replace(String message, ResourceBundle bundle)