Java Regex String Replace replace(final String template, final Map map, final String prefix, final String suffix, boolean useMapForMatching)

Here you can find the source of replace(final String template, final Map map, final String prefix, final String suffix, boolean useMapForMatching)

Description

A generic template replace function.

License

Open Source License

Parameter

Parameter Description
template a parameter
map a parameter
prefix a parameter
suffix a parameter
useMapForMatching a parameter

Declaration

public static String replace(final String template, final Map<Object, Object> map, final String prefix,
        final String suffix, boolean useMapForMatching) 

Method Source Code


//package com.java2s;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static String PROPERTY_NAME_DEF = ".+?";

    /**/*w  w w . j  a  v a 2 s  .c om*/
     * A generic template replace function. Replaces string tokens with values from map. The token
     * can have prefix or suffix. It can use prefix/suffix matching or matching using the map.
     * 
     * @param template
     * @param map
     * @param prefix
     * @param suffix
     * @param useMapForMatching
     * @return
     */
    public static String replace(final String template, final Map<Object, Object> map, final String prefix,
            final String suffix, boolean useMapForMatching) {
        final StringBuilder patternDefinition = new StringBuilder();
        if (prefix != null) {
            patternDefinition.append(createLiteralString(prefix));
        }
        patternDefinition.append("(");
        if (useMapForMatching) {
            for (final Object key : map.keySet()) {
                patternDefinition.append(key);
                patternDefinition.append("|");
                patternDefinition.append("[^\\s\\S]");
            }
        } else {
            patternDefinition.append(PROPERTY_NAME_DEF);
        }
        patternDefinition.append(")");
        patternDefinition.append(createLiteralString(suffix));

        Pattern pattern = Pattern.compile(patternDefinition.toString());
        Matcher matcher = pattern.matcher(template);
        final StringBuffer stringBuffer = new StringBuffer();
        while (matcher.find()) {
            final String key = matcher.group(1);
            Object value = map.get(key);
            if (value != null) {
                matcher.appendReplacement(stringBuffer, value.toString());
            } else {
                matcher.appendReplacement(stringBuffer, Matcher.quoteReplacement(matcher.group()));
            }
        }
        matcher.appendTail(stringBuffer);
        return stringBuffer.toString();
    }

    /**
     * Creates a literal form of a string, that can be used in regular expressions. It escapes all
     * regex special characters which are: <code>([{\$|)?*+</code>
     * 
     * @param s
     *                String to be literalized
     * @return a literalized string
     */
    public static String createLiteralString(final String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            final char c = s.charAt(i);
            switch (c) {
            case '(':
            case '[':
            case '{':
            case '\\':
            case '$':
            case '|':
            case ')':
            case '?':
            case '*':
            case '+':
            case '.':
                sb.append('\\');
                break;
            }
            sb.append(c);
        }
        return sb.toString();
    }
}

Related

  1. replace$(String text, Map vars)
  2. replace(CharSequence target, CharSequence replacement, String string)
  3. replace(final String regex, final String replacement, final StringBuffer source, boolean all)
  4. replace(Matcher m, String rv, Object value)
  5. replace(Pattern pattern, String src, Function handler)
  6. replace(String content, String name, String value)
  7. replace(String input, Pattern pattern, Function replacementGenerator)