处理思路,通过Matcher.find循环查找正则表达式,取group
@Test
public void testRegReplace() {
Pattern pattern = Pattern.compile("\\$\\{.*?\\}");
String input = "this is a properties var ${bizDate}. this is a another ${bizDate2}";
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String group = matcher.group();
System.out.println("占位字符串:" + group + "变量名," + (group.substring(2, group.length() - 1)));
System.out.println("替换表达式: " + (input.replace(group, new Date().toString())));
}
}
输出结果:
占位字符串:${bizDate}变量名,bizDate
替换表达式: this is a properties var Mon May 18 17:42:46 CST 2020. this is a another ${bizDate2}
占位字符串:${bizDate2}变量名,bizDate2
替换表达式: this is a properties var ${bizDate}. this is a another Mon May 18 17:42:46 CST 2020