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

     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+|\[|\]", "");

  

posted @ 2016-03-27 14:58  巴别塔  阅读(3873)  评论(0编辑  收藏  举报