首页 > 数据库技术 > 详细

使用正则表达式获取Sql查询语句各项(表名、字段、条件、排序)

时间:2016-03-27 15:31:17      阅读:321      评论:0      收藏:0      [点我收藏+]
     string text = "select * from [admin] where aa=1 and cc=‘b‘ order by aa desc ";
     Regex reg = null; reg = new Regex(@"\s+from\s+.*?(\s+where\s+|\s+order\s+|\s+group\s+)|\s+from\s+.+", RegexOptions.IgnoreCase);
string table = reg.Match(text).Value; table = Regex.Replace(table.ToLower(), @"\s+from\s+|\s+where\s+|\[|\]|\s+order\s+|\s+group\s+", "");
reg = new Regex(@"select\s+.*?\s+from\s+", RegexOptions.IgnoreCase); string field = reg.Match(text).Value;
field = Regex.Replace(field.ToLower(), @"select\s+|\s+from\s+|\[|\]", ""); reg = new Regex(@"\s+where\s+.*?(\s+order\s+|\s+group\s+)|\s+where\s+.+", RegexOptions.IgnoreCase); string condition = reg.Match(text).Value; condition = Regex.Replace(condition.ToLower(), @"\s+where\s+|\s+order\s+|\s+group\s+|\[|\]", ""); reg = new Regex(@"\s+order\s+by\s+.*?\s(desc|asc)|\s+order\s+by\s+.*?\s", RegexOptions.IgnoreCase); string order = reg.Match(text).Value; order = Regex.Replace(order.ToLower(),@"\s+order\s+by\s+|\[|\]", "");

  

使用正则表达式获取Sql查询语句各项(表名、字段、条件、排序)

原文:http://www.cnblogs.com/babietongtianta/p/5325696.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!