110,534
社区成员
发帖
与我相关
我的任务
分享
string test = @"{if}
fsc
{if}
ds2
{elseif}
xk00
{if}
xx
{elseif}
fd
{else}
54
{/if}
{/if}
{else}
df
{/if}";
Regex reg = new Regex(@"(?i){(/?(else)?if|else)}");
MatchCollection mc = reg.Matches(test);
foreach (Match m in mc)
{
richTextBox2.Text += "索引:" + m.Index + " 匹配内容:" + m.Value + "\n";
}
/*-------输出-------
索引:0 匹配内容:{if}
索引:13 匹配内容:{if}
索引:28 匹配内容:{elseif}
索引:46 匹配内容:{if}
索引:56 匹配内容:{elseif}
索引:70 匹配内容:{else}
索引:82 匹配内容:{/if}
索引:91 匹配内容:{/if}
索引:98 匹配内容:{else}
索引:110 匹配内容:{/if}
*/
string test = @"{if}
fsc
{elseif}
93
{elseif}
{if}
ds2
{elseif}
xk00
{if}
xx
{elseif}
fd
{else}
54
{/if}
{/if}
{else}
df
{/if}";
Regex reg = new Regex(@"(?i){(/?(else)?if|else)}");
while (Regex.Matches(test = Regex.Replace(test, @"(?is){if\b(?:(?!{/?if).)*{/if}", ""), @"(?is){if\b").Count > 1) { }
MatchCollection mc = reg.Matches(test);
foreach (Match m in mc)
{
richTextBox2.Text += "索引:" + m.Index.ToString().PadRight(3) + " 匹配内容:" + m.Value + "\n";
}
/*---------输出--------
索引:0 匹配内容:{if}
索引:11 匹配内容:{elseif}
索引:25 匹配内容:{elseif}
索引:39 匹配内容:{else}
索引:51 匹配内容:{/if}
*/
//喔 你还要它的位置
string str = "wwww{if}aaaa{if}bbbb{if}cccc{/if}dddd{/if}eeee{/if}wwww";
Regex reg = new Regex(@"((?<Open>{if})[^{]*)*((?<-Open>{/if})[^{]*?)*(?(Open){?!})");
Match match = reg.Match(str);
if (match.Success)
{
Response.Write(match.Value+"<br/>");
Regex reg_index = new Regex("({if}|{/if})");
MatchCollection match_index = reg_index.Matches(str);
foreach (Match m in match_index)
{
CaptureCollection cc = m.Groups[1].Captures;
foreach (Capture c in cc)
{
Response.Write(c.Index+"<br/>");
}
}
}
/*输出
{if}aaaa{if}bbbb{if}cccc{/if}dddd{/if}eeee{/if}
4
12
20
28
37
46
*/
string str = "wwww{if}aaaa{if}bbbb{if}cccc{/if}dddd{/if}eeee{/if}wwww";
Regex reg = new Regex(@"((?<Open>{if})[^{]*)*((?<-Open>{/if})[^{]*?)*(?(Open){?!})");
MatchCollection match = reg.Matches(str);
foreach (Match m in match)
{
Response.Write(m.Value);
}
查询字符:
{if}
fsc
{if}
ds2
{elseif}
xk00
{if}
xx
{elseif}
fd
{else}
54
{/if}
{/if}
{else}
df
{/if}