110,539
社区成员
发帖
与我相关
我的任务
分享
Match m = Regex.Match(test.Trim(), @"^<%IF((?!%>).)*%>(((?<o>)<%IF((?!%>).)*%>|(?<-o>)<%/IF%>|(?:(?!<%/?IF.*?%>).))*)(?(o)(?!))<%/IF%>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (m.Success)
richTextBox2.Text += "匹配成功:\n" + m.Value + "\n";
else
richTextBox2.Text += "匹配不成功";
Match m = Regex.Match(test.Trim(), @"<%IF.*?%>(((?<o>)<%IF.*?%>|(?<-o>)<%/IF%>|(?:(?!<%/?IF.*?%>).))*)(?(o)(?!))<%/IF%>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if(m.Success)
richTextBox2.Text += "匹配成功:\n"+m.Value + "\n";
else
richTextBox2.Text += "匹配不成功";
string test = @"
<%IF 10%3=1%>
<%IF
""hello""=""hello""%>
asdf
<%/IF%>
<%ELSE%>
12
34
<%/IF%>
abcde
<%IF test%>one test <%/IF%>
fghij
<%IF 3+3=5%>asfdsf
<%IF true%>fasdf <%/IF%>
<%IF false%>1234 <%/IF%>
klmno
<%IF test%>another test <%/IF%> ";
Match m = Regex.Match(test, @"(?<=^(?:(?!<%/?IF.*?%>).)*)(<%IF.*?%>(((?<o>)<%IF.*?%>|(?<-o>)<%/IF%>|(?:(?!<%/?IF.*?%>).))*)(?(o)(?!))<%/IF%>)", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if(m.Success)
richTextBox2.Text += "匹配成功:\n"+m.Value + "\n";
else
richTextBox2.Text += "匹配不成功";
string test = @"
<%IF 3+2=5%>
<%IF
""hello""=""hello""%>
asdf
<%/IF%>
<%ELSE%>
12
34
<%/IF%>
abcde
<%IF test%>one test <%/IF%>
fghij
<%IF 3+3=5%>asfdsf
<%IF true%>fasdf <%/IF%>
<%IF false%>1234 <%/IF%>
klmno
<%IF test%>another test <%/IF%> ";
Regex reg = new Regex(@"^(?:(?!<%/?IF[^%]*%>)[\s\S])*(<%IF[^%]*%>(((?<o>)<%IF[^%]*%>|(?<-o>)<%/IF%>|(?:(?!<%/?IF[^%]*%>)[\s\S]))*)(?(o)(?!))<%/IF%>)", RegexOptions.IgnoreCase);
while (reg.IsMatch(test))
{
richTextBox2.Text += "此处格式正确,IF结构的内容为:\n" + reg.Match(test).Groups[1].Value + "\n\n";
test = reg.Replace(test, "");
}
if (!Regex.IsMatch(test, @"<%/?IF[^%]*%>", RegexOptions.IgnoreCase))
{
richTextBox2.Text += "没有错误!";
}
else
{
richTextBox2.Text += "有错误,含有错误的文本为:\n" + test.Trim();
}
string[] str = new string[]{@"
<%IF 3+2=5%>
<%IF
""hello""=""hello""%>
asdf
<%/IF%>
<%ELSE%>
12
34
<%/IF%>",@"<%IF 3+3=5%>asfdsf
<%IF true%>fasdf <%/IF%>
<%IF false%>1234 <%/IF%>", "<%IF test%>test <%/IF%>"};
foreach (string s in str)
{
Match m = Regex.Match(s, @"(?<=^\s*)<%IF[^%]*%>(((?<o>)<%IF[^%]*%>|(?<-o>)<%/IF%>|(?:(?!<%/?IF[^%]*%>)[\s\S]))*)(?(o)(?!))<%/IF%>(?=\s*$)", RegexOptions.IgnoreCase);
if (m.Success)
{
richTextBox2.Text += "符合条件,其内容为:\n";
richTextBox2.Text += m.Value + "\n\n";
}
else
richTextBox2.Text += "不符合条件\n\n";
}
//saucer的代码
Regex re = new Regex("<%IF(?>(<%IF(?<DEPTH>)(?<CONTAIN>)|<%/IF(?<-DEPTH>)|.))*(?(DEPTH)(?!))(?(CONTAIN)|(?!))<%/IF%>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
string s = @"
<%IF 3+2=5%>
<%IF
""hello""=""hello""%>
asdf
<%/IF%>
<%ELSE%>
12
34
<%/IF%>
<%IF 3+3=5%>asfdsf
<%IF true%>fasdf <%/IF%>
<%IF false%>1234 <%/IF%> ";
MatchCollection mc = Regex.Matches(s, @"<%IF(((?<o>)<%IF|(?<-o>)<%/IF|(?<c>)<%ELSE|(?:(?!<%/?IF)[\s\S]))*)(?(o)(?!))(?(c)|(?!))<%/IF%>", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
richTextBox2.Text += m.Value + "\n";
}
/*输出
<%IF 3+2=5%>
<%IF
"hello"="hello"%>
asdf
<%/IF%>
<%ELSE%>
12
34
<%/IF%>
/*