oracle to_date批量替换
具体问题具体分析,本案例仅供参考。
问题:
因需求要大量替换类型以下sql语句:
INSERT INTO test VALUES ('test', NULL, to_date('2021-01-04 20:42:17', to_date('2021-01-04 20:42:17',null; INSERT INTO test VALUES ('test', NULL, to_date('2021-01-04 20:42:17', to_date('2021-01-04 20:42:17',null;
解决方案:
正则替换相应字符:
1.搜字符串在线转义,将sql转化为字符串
2.编程C#程序
using System.Text.RegularExpressions; var strs = "INSERT INTO test VALUES ('test', NULL, to_date('2021-01-04 20:42:17', to_date('2021-01-04 20:42:17',null);\n" + "INSERT INTO test VALUES ('test', NULL, to_date('2021-01-04 20:42:17', to_date('2021-01-04 20:42:17',null);"; var ary = strs.Split(";"); var matter = @"to_date\W+\S*\w\s[0-9]+:[0-9]+:[0-9]+\S+"; var regex = new Regex(matter); var result = regex.Matches(strs).Select(p => p.Value).Distinct().ToList(); for (int j = 0; j < result.Count; j++) { strs = strs.Replace(result[j], ", " + result[j] + "'yyyy-mm-dd hh24:mi:ss'),"); } var newStrs = strs.Replace(", , to_date(", ", to_date("); Console.WriteLine(newStrs); Console.ReadKey();
3.最终结果:
好好学习,天天向上。