提取[]中的字符string cutText = "已知的字符串";
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\[(?<str>[.]+)\]+");
System.Text.RegularExpressions.Match match  = regex.Match(cutText);
if(match.Success)
{
MessageBox.Show(match.Groups["str"].Value);
}

解决方案 »

  1.   

    同样想提取[]前面的字符,只需要得到match.Index,
    然后利用string.substring(0,match.Index)就可以获得前面的字符
      

  2.   

    不过要是字符串里面只有一个[]的话,用String.IndexOf('[']);和IndexOf(']')获得两个索引,然后切割字符串就更简单了
      

  3.   

    StreamReader sr = new StreamReader("yourfile", System.Text.Encoding.Default);
    string s;
    Regex re = new Regex("^([^]]+)\[[^]+]\]");
    while ((s = sr.ReadLine()) != null)
    {}
      

  4.   

    对不起,请忽略上面的回贴,这里是正解 StreamReader sr = new StreamReader("yourfile.txt", System.Text.Encoding.Default);
    string s;
    Regex re = new Regex(@"^([^[]+)\[([^]]+)\]");
    while ((s = sr.ReadLine()) != null)
    {
       Match m = re.Match(s);
       if (m.Success)
    Console.WriteLine("{0} -- {1}", m.Groups[1].Value, m.Groups[2].Value);
    }
      

  5.   

    to saucer(思归):
    谢谢,我试试。
    你的解答正是我想要的,我希望一次能把[]前面和里面的字符提取出来,现在我要处理的是一个多行的Textbox的文本,好象正则表达式有个什么Multiline 选项.怎么用它?
      

  6.   

    我已能提取第一段的信息,但是怎么提取第二段呢?我这样写不行呀:
    Match m = re.Match(s);
    while (m.Success)
    {
    s1= m.Groups[1].Value+ m.Groups[2].Value;
    m.NextMatch();
    }
      

  7.   

    string s,s1;
    Regex re = new Regex(@"^([^[]+)\[([^]]+)\]");
    s=this.TextBox1.Text;

    MatchCollection mc;
    mc=re.Matches(s);
    for (int i=0;i < mc.Count;i++)
    {
    s1=mc[i].Groups[1].Value+":"+mc[i].Groups[2].Value;
    }