我有这个.NET正则表达式:
(<%#)(?<type>((sql)|(magic)|(sqlscalar))):(?<method>([^\s]*)\s*)(\s)*(%>)
它与'sqlscalar'
和'magic'
完全匹配,但与'sql'
不匹配
任何帮助表示赞赏..
这是服务器名称&lt;%#sqlscalar:@@ servername%&gt;这是来自funcA的结果,其中@p值= 100&lt;%#sqlscalar:dbo.funcA(&lt;%variableA%&gt;)%&gt; :上面的两个函数都在第一行, FuncB的结果和第二行中的fMagic()&lt;%#sqlscalar:funcB(&lt;%variableB%&gt;)%&gt; &LT;%#魔:fMagic()%&GT; 第三行来自sql'select * from dbo.ftelUsers()'&lt;%#sql:select * from dbo.ftElUsers()%&gt;
答案 0 :(得分:2)
你的正则表达式不允许冒号后的空格(除非它直接在结束括号%>
之前)。这就是select * from...
不匹配的原因。
我提出了一个不同的正则表达式(更新来处理之前我忽略过的嵌套标签):
Regex regexObj = new Regex(
@"<%[#] # Match '<%#'
(?<type> # Match and capture in group 'type':
sql # 'sql'
(?:scalar)? # optionally followed by 'scalar'
| # OR
magic # 'magic'
) # End of group 'type'
: # Match ':'
(?<method> # Match and capture in group 'method'
(?> # Either match (possessively):
(?: # the following group which matches
(?!<%|%>) # only if we're not at the start of <% or %>
. # any character
)+ # once or more
| # or
<% (?<Depth>) # <% (and increase the nesting counter)
| # or
%> (?<-Depth>) # %> (and decrease the nesting counter).
)* # Repeat as needed.
(?(Depth)(?!)) # Assert that the nesting counter is at zero.
) # End of group 'method'
%> # Then match a closing %>.
",
RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);