@Test
public void replaceStr() {
System.out.println("将{gid}替换为某值");
String name1 = "name";
String cql = "MATCH p=({gid: {gid}})-[r:paper_yantian1586528279416]->() RETURN p LIMIT 25 ";
System.out.println(cql.replaceAll("\\{[gid^}]*\\}", "\""+name1+"\"}"));
System.out.println("将{name}替换为某值,{name}中的name可动态传值被替换");
String sql = "select name from person where name = {name} and sex = %s";
String name = "name";
int val = 123;
System.out.println(sql.replaceAll("\\{["+name+"^}]*\\}", val+""));
System.out.println("暴力替换");
System.out.println(sql.replace("{name}", "123"));
System.out.println("去除花括号并赋值");
String str = "{f.name} and {f.age}";
String pattern = "\\{[^}]*\\}";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(str);
while (m.find()) {
String g = m.group();
String target = g.substring(1, g.length() - 1);//去掉花括号
str = str.replace(g,target + " <= '2019-09-09'");
}
System.out.println("str:" + str);
}
执行结果