110,533
社区成员
发帖
与我相关
我的任务
分享
string str = "aaa[bbb|http://www.csdn.com]aaa[hhhh[bbb|http://www.csdn.com]";
Regex reg = new Regex(@"(((?<Open>\[)([^\[\]]*))*((?<-Open>\])[^\[\]]*)*)*(?(Open)(?!))");
foreach (Match m in reg.Matches(str))
{
string[] ss = m.Groups[3].Value.Split('|');
foreach (string s in ss)
{
Response.Write(s);
Response.Write("<br/>");
}
}
/*
bbb
http://www.csdn.com
bbb
http://www.csdn.com
*/
string s = "aaa[bbb|http://www.csdn.com]aaa[hhhh[bbbc|http://www.csdn.com]";
Regex re = new Regex(@".*?(\[|\[(.*?))(?<result>\w+?)\|(?<result2>.*?)\].*?");
MatchCollection result = re.Matches(s);
foreach (Match item in result)
{
Console.WriteLine(item.Groups["result"] + "\n" + item.Groups["result2"]);
}
Console.ReadLine();
string str = @"aaa[bbb|http://www.csdn.com]";
string[] result = new Regex(@"(?is)(?<=\[)\S+(?=\])").Match(str).Value.Split('|');
result[0] //bbb
result[1]; //http...
Match m = Regex.Match(yourStr,@"\[([^|]+)\|([^\]]+)\]");
m.Groups[1].Value;//就是你要的第一部分
m.Groups[2].Value;//就是你要的第二部分