Java Regex String Replace replace(String s, Properties p)

Here you can find the source of replace(String s, Properties p)

Description

replace

License

Open Source License

Declaration

public static String replace(String s, Properties p) 

Method Source Code

//package com.java2s;
/* //from w  ww .  jav  a2  s. c o m
 * @(#)StringUtil.java 1.0 2004-10-11
 *
 * Copyright 2005 UFIDA Software Co. Ltd. All rights reserved.
 * UFIDA PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.util.Properties;

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

public class Main {
    public static String replace(String s, Properties p) {
        String regex = "\\$\\w+\\W|\\$\\{[^}]+\\}";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(s);

        while (m.find()) {
            String temp = m.group();
            String key = null;
            if (temp.indexOf("{") != -1) {
                key = temp.substring(temp.indexOf("{") + 1, temp.length() - 1);

            } else {
                key = temp.substring(1, temp.length() - 1);
            }
            String value = p.getProperty(key);

            if (value != null) {
                s = s.replace(temp, value);
                m = pattern.matcher(s);
            }
        }

        return s;
    }
}

Related

  1. replace(String message, ResourceBundle bundle)
  2. replace(String operateOn[], String from, String to)
  3. replace(String original, CharSequence target, CharSequence replacement)
  4. replace(String pattern, String replace, String s)
  5. replace(String s, Pattern pattern, Function func)
  6. replace(String s1, String s2, Pattern pattern)
  7. replace(String source, String prefix, String suffix, String prefixReplace, String suffixReplace)
  8. replace(String source, String search, String replace)
  9. replace(String source, String search, String replacement)