如:字符串 ABCD[EF]GH[IJ]K 中, 如何提取[]中的字符串EF 和IJ?

解决方案 »

  1.   

    正则比较简单,参考如下代码:
    //using System.Text.RegularExpressions;
    string s = "ABCD[EF]GH[IJ]K";
    foreach (Match vMatch in Regex.Matches(s, @"\[(?<Text>[^\]]+)\]"))
    {
        Console.WriteLine(vMatch.Result("${Text}"));
    }
      

  2.   

    Regex regex = new Regex(@"\[<result>\]");
    MatchCollection matchesFound;
    matchesFound = regex.Matches(sInputString);
    foreach (Match matchMade in matchesFound)
    {
    Console.Write(matchMade.Groups[0].Value);
    }