62,041
社区成员
发帖
与我相关
我的任务
分享
void Main()
{
string s="[a]+[b]*[c]+sum(([d]+[e])*[f])";
Match m =Regex.Match(s,@"(\[([^]]+)\][^\[\]])+sum(\(((?<o>\()|(?<-o>)\)|[^()]+)*(?(o)(?!))\))");
foreach(Capture c in m.Groups[2].Captures)
{
Console.Write("{0} - ",c.Value);
}
Console.WriteLine(m.Groups[3].Value);
//a - b - c - (([d]+[e])*[f])
}
static void RegexBalance1()
{
string txt = @"[a]+[b]*[c]+sum(([e]+[c])*[c])+([x]+[y])*[z]";
string patten = @"(?<=\w\()[^\(\)]*(((?'op'\()[^\(\)]*)+((?'-op'\))[^\(\)]*)+)*(?(op)(?!))[^\(\)]*(?=\))";
Regex.Matches(txt, patten).Cast<Match>().ToList().ForEach(x =>
{
Console.WriteLine(x.Value);
});
Regex.Matches(Regex.Replace(txt, patten, string.Empty), @"(?<=\[).*?(?=\])").Cast<Match>().ToList().ForEach(x =>
{
Console.WriteLine(x.Value);
});
}
不是很靠谱的方案