码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2016-03-27 15:32:29      阅读:181      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!