java中带回调函数的字符串替换,类似js中的replace(rgExp,function)
https://blog.csdn.net/weixin_33834075/article/details/91599852
import java.util.regex.Matcher; import java.util.regex.Pattern; public class TestRegExp { public static void main(String[] args) { String str = "insert into @@table(@@dataAsIn1sertKeys:aaa.aa) values( @@dataAsInsertValues )"; System.out.println(replaceString(str)); } public static String replaceString(String str) { Pattern pattern = Pattern.compile("@@[0-9a-zA-Z:.]+"); Matcher m = pattern.matcher(str); if (!m.find()) { return str; } StringBuffer sb = new StringBuffer(); int index = 0; do { index++; String group0 = m.group(0); System.out.println(group0); m.appendReplacement(sb, "aaaa_" + index + "_" + m.group(0)); } while (m.find()); m.appendTail(sb); return sb.toString(); } }