str="[111]jdfjf34[drr]4545[erer][9rfj0r]";
要求将以[]为标志的分割字符串
即:分割成[111], [drr],[erer],[9rfj0r]这样4个字串
谢谢

解决方案 »

  1.   


    string str = "[111]jdfjf34[drr]4545[erer][9rfj0r]";
    string pattern = @"\[(?<region>.*?)\]";
    Regex regex = new Regex(pattern);
    for (Match match = regex.Match(str); match.Success; match = match.NextMatch())
    {
    Console.WriteLine(string.Format("[{0}]", match.Groups["region"].Value));
    }
      

  2.   

    用正则using System;using System.Text.RegularExpressions;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    string subjectString = "[111]jdfjf34[drr]4545[erer][9rfj0r]";
                    Regex regexObj = new Regex(@"(\[[^\]]*\])");
                    Match matchResults = regexObj.Match(subjectString);
                    while (matchResults.Success)
                    {
                        // matched text: matchResults.Value
                        // match start: matchResults.Index
                        // match length: matchResults.Length
                        Console.WriteLine(matchResults.Value);
                        matchResults = matchResults.NextMatch();
                    }
                }
                catch (ArgumentException ex)
                {
                    // Syntax error in the regular expression
                }            
            }
        }
    }