求一分行正则表达式: 
以回车为分行符。如果回车字符出现在双引号内,不分行。 
如:aaa "回车"bcdd回车,算作一行。 

解决方案 »

  1.   

    用正则恐怕难以实现。我写了个不用正则的:using System; 
    using System.Text;
    using System.Collections.Generic; class Program 

      static void Main(string[] args) 
      {
        string  ss = "\nab\"1\n2\"cd\nef";
        string[] t = Split(ss, '\n');
        foreach (string s in t)
        {
          Console.WriteLine("[{0}]", s);
        }
      }
      
      // 将 input 按 separator 拆分,双引号内的 separator 不算隔符
      static string[] Split(string input, char separator)
      {
        List<string> t = new List<string>();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < input.Length; i++)
        {
          if (input[i] == separator)
          {
            t.Add(sb.ToString());
            sb.Length = 0;
          }
          else if (input[i] == '"')
          {
            sb.Append(input[i]);
            for (i++; i < input.Length; i++)
            {
              sb.Append(input[i]);
              if (input[i] == '"') break;
            }
          }
          else sb.Append(input[i]);
        }
        t.Add(sb.ToString());
        return t.ToArray();
      }
    }
    /* 程序输出:
    []
    [ab"1
    2"cd]
    [ef]
    */
      

  2.   

    感谢wuyi8808 ,应该是一种可行方法。我想如果正则能实现,可能效率更高一些。
      

  3.   

     string s = "aaa \"ccc\nbb\nb\"bcdd\ncccccc\naaaa";
                string sPtn = @"""[^""]*""(((?'set'"")[^""]*)+((?'-set'"")[^""]*)+)*(?(set)(?!))";
                Match m = Regex.Match(s, sPtn);
                while (m.Success)
                {
                    s = s.Replace(m.Value, Regex.Replace(m.Value, "\n+", ""));
                    m = m.NextMatch();
                }
                Console.WriteLine(s);
                /*
                aaa "cccbbb"bcdd
                cccccc
                aaaa
                */
      

  4.   

    不知这样算不算你要求的用正则解这道题:using System; 
    using System.Text.RegularExpressions;class Program 

      static void Main(string[] args) 
      {
        string  ss = "\nab\"1\n2\"cd\nef";
        foreach (Match m in Regex.Matches(ss, @"(?:""[^""]*""|.)+"))
        {
          Console.WriteLine("[{0}]", m);
        }
      }
    }
    /* 程序输出:
    [ab"1
    2"cd]
    [ef]
    */
      

  5.   


    string[] str = Regex.Split(input,"$",RegexOptions.Multiline);