处理思路,通过Matcher.find循环查找正则表达式,取group

    1. @Test
    2. public void testRegReplace() {
    3. Pattern pattern = Pattern.compile("\\$\\{.*?\\}");
    4. String input = "this is a properties var ${bizDate}. this is a another ${bizDate2}";
    5. Matcher matcher = pattern.matcher(input);
    6. while (matcher.find()) {
    7. String group = matcher.group();
    8. System.out.println("占位字符串:" + group + "变量名," + (group.substring(2, group.length() - 1)));
    9. System.out.println("替换表达式: " + (input.replace(group, new Date().toString())));
    10. }
    11. }
    12. 输出结果:
    13. 占位字符串:${bizDate}变量名,bizDate
    14. 替换表达式: this is a properties var Mon May 18 17:42:46 CST 2020. this is a another ${bizDate2}
    15. 占位字符串:${bizDate2}变量名,bizDate2
    16. 替换表达式: this is a properties var ${bizDate}. this is a another Mon May 18 17:42:46 CST 2020