Java Regex String Replace replace(String string, Properties table)

Here you can find the source of replace(String string, Properties table)

Description

Equivalent to replace(string, table, false).

License

Open Source License

Parameter

Parameter Description
string the String to be processed.
table the replacement strings

Return

a new string with the identifiers replaced with values from the table.

Declaration

public static String replace(String string, Properties table) 

Method Source Code

//package com.java2s;
/*---------------------------------------------------------------
*  Copyright 2005 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**//from  ww w  . j  av a2  s  .  com
     * Equivalent to <code>replace(string, null, true)</code>.
     * @param string the String to be processed.
     * @return a new string with the identifiers replace with values
     * from the system environment.
     */
    public static String replace(String string) {
        return replace(string, null, true);
    }

    /**
     * Equivalent to <code>replace(string, table, false)</code>.
     * @param string the String to be processed.
     * @param table the replacement strings
     * @return a new string with the identifiers replaced with values
     * from the table.
     */
    public static String replace(String string, Properties table) {
        return replace(string, table, false);
    }

    /**
     * Replace coded identifiers with values from a table.
     * Identifiers are coded as ${name}. The identifier is replaced
     * by the string value in the table, using the name as the key.
     * Identifiers which are not present in the table are left
     * unmodified.
     * @param string the String to be processed
     * @param table the replacement strings (this argument can be null)
     * @param includeEnvironmentVariables true to include the System
     * environment variables in the replacement table
     * @return a new string with the identifiers replaced with values
     * from the table.
     */
    public static String replace(String string, Properties table, boolean includeEnvironmentVariables) {
        try {
            Pattern pattern = Pattern.compile("\\$\\{\\w+\\}");
            Matcher matcher = pattern.matcher(string);
            StringBuffer sb = new StringBuffer();
            while (matcher.find()) {
                String group = matcher.group();
                String key = group.substring(2, group.length() - 1).trim();
                String repl = null;
                if (table != null)
                    repl = table.getProperty(key);
                if ((repl == null) && includeEnvironmentVariables)
                    repl = System.getenv(key);
                if (repl == null)
                    repl = Matcher.quoteReplacement(group);
                matcher.appendReplacement(sb, repl);
            }
            matcher.appendTail(sb);
            return sb.toString();
        } catch (Exception ex) {
            return string;
        }
    }

    /**
     * Trim a string, returning the empty string if the supplied string is null.
     * @param text the text to trim.
     * @return the trimmed string.
     */
    public static String trim(String text) {
        if (text == null)
            return "";
        return text.trim();
    }
}

Related

  1. replace(String source, String[] patterns, String[] replacements)
  2. replace(String str, String key, String value)
  3. replace(String str, String replacement, Function func)
  4. replace(String string, Pattern pattern, String replacement)
  5. replace(String string, Pattern[] patterns, String[] replacements)
  6. replace(String string, String pattern, String replacement, boolean literal)
  7. replace(String text, Map vars, String prefix)
  8. replace(String text, String find, String match, boolean useRegex, boolean isCaseSensitive)
  9. replace(String text, String findPattern, String replacePattern)