日期:2014-05-18  浏览次数:21104 次

如何用正则表达式提取所有sql参数化语句的变量?
如where UserName=@User_$#@Name,提取出@User_$#@Name
参数化命名规则
只能包含字母、数字和_、$、#、@四个特殊字符,并且@不能在在开头同时存在,即@@User_$#@Name就不行(防止和select @@identity冲突)@User_$#@@Name也是可以的

网上找到的一种,但是没有考虑特殊字符的问题

List<string> result = new List<string>();
Regex paramReg = new Regex(@"[^@@](?<sqlParam>@\w+)");
MatchCollection matches = paramReg.Matches(sql);
foreach (Match m in matches)
  Response.Write(m.Groups["sqlParam"].Value + ",");

正则表达式不是很清楚,希望大家帮忙下。

------解决方案--------------------
+1 等正则高手
------解决方案--------------------
探讨

引用:

引用:

@"(?<=[^0-9a-zA-Z])@(?!@)[0-9a-zA-Z_$#@]+"


Regex paramReg = new Regex(@"(?<=[^0-9a-zA-Z])@(?!@)[0-9a-zA-Z_$#@]+");
这样提取出来只有,号了。
Regex paramReg = new Regex(@"[^@……

------解决方案--------------------
怎么可能只得到逗号呢,我给你的那个表达式根本匹配不到逗号。
------解决方案--------------------
探讨
引用:

试试
=\s*@((?!@)[a-zA-Z0-9_$#@]+)(\s|$)


Regex paramReg = new Regex(@"=\s*@((?!@)[a-zA-Z0-9_$#@]+)(\s|$)");
这样也只剩下,号了。