求一正则表达式,大牛进~~

NqIceCoffee 2013-12-17 10:45:08
string s = "select * from tableName where type=type1 oder by id DESC";
string pattern = @"^select\s+(?:top\s+(\d+)\s+)?(.+)\s+from\s+([^\s]+)(?:\s+where\s*(.+)?(?:\s+group\s+by\s+(.+)\s+)?(?:\s+order\s+by\s+(.+)\s+)?)?$";

s = Regex.Replace(s, @"\s{2,}", " ");
s = s.Replace(", ", ",").Trim();

Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
var match = reg.Match(s);

if (match.Success)
{
string n = match.Groups[1].ToString();
string fields = match.Groups[2].ToString();
string tableName = match.Groups[3].ToString();
string where = match.Groups[4].ToString();
string group = match.Groups[5].ToString();

this.message.Text = string.Format("{0}<br />{1}<br />{2}<br />{3}<br />{4}", n, fields, tableName, where, group);
}


简单的说一下需求,就是实现SQL语句中提取非关键字的部分(仅考虑单语句,不考虑子查询等情况)
以上是我写的一部分代码,提取不出来Group By 和Order By的部分
请大牛帮忙~~~100分
...全文
150 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2013-12-17
  • 打赏
  • 举报
回复
引用 4 楼 NqIceCoffee 的回复:
string s = "select * from tableName where type=type1 and type='order' order by id DESC";
TO:版主 貌似这样的SQL匹配不了
这是单引号没有匹配到的原因,try:
string pattern = @"^select\s+(?:top\s+(\d+)\s+)?(.+)\s+from\s+(\w+)\s+(where\s(((?!(?:order|group))[\s\S])+))?((?:order|group) by .+)?$";
NqIceCoffee 2013-12-17
  • 打赏
  • 举报
回复
引用 3 楼 q107770540 的回复:
update:
string pattern = @"^select\s+(?:top\s+(\d+)\s+)?(.+)\s+from\s+(\w+)\s+(where\s(((?!(?:order|group)).)+))?((?:order|group) by .+)?$";
版版还有好办法么有?
NqIceCoffee 2013-12-17
  • 打赏
  • 举报
回复
string s = "select * from tableName where type=type1 and type='order' order by id DESC";
TO:版主 貌似这样的SQL匹配不了
q107770540 2013-12-17
  • 打赏
  • 举报
回复
update:
string pattern = @"^select\s+(?:top\s+(\d+)\s+)?(.+)\s+from\s+(\w+)\s+(where\s(((?!(?:order|group)).)+))?((?:order|group) by .+)?$";
q107770540 2013-12-17
  • 打赏
  • 举报
回复
		s = Regex.Replace(s, @"\s+", " ");
结果: <br />fields=*<br />tableName=tableName<br />where=type=type1 <br />group=order by id DESC
q107770540 2013-12-17
  • 打赏
  • 举报
回复
BTW: 你的SQL语句有问题 : oder ==> order
string s = "select * from tableName where type=type1 order by id DESC";
string pattern = @"^select\s+(?:top\s+(\d+)\s+)?(.+)\s+from\s+(\w+)\s+(where\s(((?!(?:order|group)).)+))?((?:order|group) by .+$)";
 
			s = Regex.Replace(s, @"\s{2,}", " ");
			s = s.Replace(", ", ",").Trim();
 
			Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
			var match = reg.Match(s);
 
			if (match.Success)
			{
				string n = match.Groups[1].ToString();
				string fields = match.Groups[2].ToString();
				string tableName = match.Groups[3].ToString();
				string where = match.Groups[5].ToString();
				string group = match.Groups[7].ToString();
 
			string result=string.Format("{0}<br />fields={1}<br />tableName={2}<br />where={3}<br />group={4}", n, fields, tableName, where, group);
		 
			}
NqIceCoffee 2013-12-17
  • 打赏
  • 举报
回复
引用 8 楼 q107770540 的回复:
TKS 版版
q107770540 2013-12-17
  • 打赏
  • 举报
回复
string s = "select * from tableName where type=type1 and type='order' order by id DESC";
string pattern = @"^select\s+(?:top\s+(\d+)\s+)?(.+)\s+from\s+(\w+)\s*(where\s(((?!(?:order|group) by)[\s\S'])+))?((?:order|group) by .+)?$";
 
			s = Regex.Replace(s, @"\s+", " ");
			s = s.Replace(", ", ",").Trim();
 
			Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
			var match = reg.Match(s);
 
			if (match.Success)
			{
				string n = match.Groups[1].ToString();
				string fields = match.Groups[2].ToString();
				string tableName = match.Groups[3].ToString();
				string where = match.Groups[5].ToString();
				string group = match.Groups[7].ToString();
 
			string result=string.Format("{0}<br />fields={1}<br />tableName={2}<br />where={3}<br />group={4}", n, fields, tableName, where, group);
		 
			}
NqIceCoffee 2013-12-17
  • 打赏
  • 举报
回复
引用 6 楼 q107770540 的回复:
string pattern = @"^select\s+(?:top\s+(\d+)\s+)?(.+)\s+from\s+(\w+)\s+(where\s(((?!(?:order|group))[\s\S])+))?((?:order|group) by .+)?$";
这个貌似还是不行哦

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧