Java Regex String Replace replace(String val)

Here you can find the source of replace(String val)

Description

Replaces any ${} strings with their corresponding environent variable.

License

Open Source License

Parameter

Parameter Description
val a parameter

Declaration

public static String replace(String val) 

Method Source Code


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

public class Main {
    private static final Pattern p = Pattern.compile("[$][{]([^}]+)[}]");

    /**/*from   w w  w  . j  a v  a 2s. c om*/
     * Replaces any ${} strings with their corresponding environent variable.
     *
     * @param val
     * @return
     */
    public static String replace(String val) {
        Matcher matcher = p.matcher(val);
        StringBuffer buf = new StringBuffer();
        while (matcher.find()) {
            String envVar = matcher.group(1);
            String envVal = System.getProperty(envVar);
            if (envVal == null)
                envVal = "NOT-SPECIFIED";
            matcher.appendReplacement(buf, envVal.replace("\\", "\\\\"));
        }
        matcher.appendTail(buf);
        return buf.toString();
    }
}

Related

  1. replace(String text, Map vars, String prefix)
  2. replace(String text, String find, String match, boolean useRegex, boolean isCaseSensitive)
  3. replace(String text, String findPattern, String replacePattern)
  4. replace(String text, String targetText, String newText)
  5. replace(String text, String[] searchStrings, String[] replacements)
  6. replace(String value)
  7. replace_values(HashMap values, String str)
  8. replace_with_in(String aStringToReplace, String aNewString, String aString)