正则替换 sql语法 去掉字段中的引号,请大家帮帮我

例如有一条插入语句
insert into table("id","name","remark") values ('1','test','[{"key1":"value1","key2":"value2"}]')
把字段中的引号去掉,替换后 (有多张表,表字段不固定有几个)
insert into table(id,name,remark) values ('1','test','[{"key1":"value1","key2":"value2"}]')

我的思路是 取 insert 和 values 之间的所有引号,但是这个不知道怎么写,有没有知道的

题主试试,测试正常。有帮助或启发麻烦点个采纳【本回答右上角】,谢谢~~

img

import java.util.regex.Pattern;
import java.util.regex.Matcher;
class t{
  public static void main(String[]args){
      String s="insert into table(\"id\",\"name\",\"remark\") values ('1','test','[{\"key1\":\"value1\",\"key2\":\"value2\"}]')\n"+
      "insert into table(\"id\",\"name\",\"remark\") values ('1','test','[{\"key1\":\"value1\",\"key2\":\"value2\"}]')\n"+
      "insert into table(\"id\",\"name\",\"remark\") values ('1','test','[{\"key1\":\"value1\",\"key2\":\"value2\"}]')\n";
      
      System.out.println(s);
      System.out.println("--------------------");

      String pattern = "\\([^\\)]+\\)\\s*values";
      Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
      Matcher m = p.matcher(s);
      StringBuffer sb = new StringBuffer();
      while (m.find())
      {
            String key = m.group(0);
            m.appendReplacement(sb, key.replace("\"",""));
      }
      m.appendTail(sb);
      s=sb.toString();
      System.out.println(sb.toString());
  }
}

建议你先提取字段 ["id","name","remark"],然后直接替换掉引号再拼接

可以先用此正则表达式^insert.+values$将insert和values只间的字符串匹配截取出来,再用s.replace(""","")得到想要的字符串前半段,再和之前的后面那部分拼起来

按照你的思路: 取 insert 和 values 之间的所有引号
1、先找到values 在哪
indexof(str,'values')
2、拿到values之前的部分
substr(str,0,indexof(str,'values'))
3、替换双引号为空
replace(substr(str,0,indexof(str,'values')),'"','')
4、拿到values后面的部分
substr(str,indexof(str,'values'))
5、拼接、
concat(replace(substr(str,0,indexof(str,'values')),'"','') , substr(str,indexof(str,'values')))