如何取出中括号中的字符,比如:A[A1],B[B1],C[C1],取得A1 B1 C1,取得后再将这些字符(包括中括号)从原字符中删除,最后得到的字符串是A,B,C

解决方案 »

  1.   

                Regex re = new Regex(@"\[[^\]]+\]");
                string oldstr = @"A[A1],B[B1],C[C1]";
                string newstr = re.Replace(oldstr, "");
                Console.WriteLine(newstr);
      

  2.   

    取出的值
    re.Match(oldstr).Groups[0]
    re.Match(oldstr).Groups[1]
    re.Match(oldstr).Groups[2]
      

  3.   

    你到底什么目的,不同的目的,写法不同的是替换,还是提取,还是两者都要如果只是替换
    string test = "A[A1],B[B1],C[C1]";
    richTextBox1.Text = Regex.Replace(test, @"\[[^\]]*\]", "");如果只是提取
    string test = "A[A1],B[B1],C[C1]";
    MatchCollection mc = Regex.Matches(test, @"\[([^\]]*)\]");
    foreach (Match m in mc)
    {
        richTextBox1.Text += m.Groups[1].Value + "\n";
    }如果两者都要
    string test = "A[A1],B[B1],C[C1]";
    list = new List<string>();
    test = Regex.Replace(test, @"\[([^\]]*)\]", new MatchEvaluator(myEvaluator));
    richTextBox1.Text += test + "\n";
    foreach (string s in list)
    {
        richTextBox1.Text += s + "\n";
    }
    List<string> list;
    private string myEvaluator(Match m)
    {
        list.Add(m.Groups[1].Value);
        return "";
    }