有一个字符串, a "ha ha" ws怎样按照空格分隔成三个字符串(注: 如果字符串用引号括起来了, 就不分隔)应该分隔成下面三个
1. a
2. ha ha
3. ws

解决方案 »

  1.   

    那就用引号分割好了
    string[] strs = str.Split('\"');
    strs[0].Trim(), strs[1].Trim().Replace("\"",""), strs[2].Trim()就是你想要的东东了
      

  2.   

    用引号分隔的话, 加入字符串没有引号, 不是又错了吗比如  a ha ha ws, 咋办
      

  3.   

    空格的前一个“ 的index是偶数 则不记录,否则记录index .
    最后按index去截取。试试看看
      

  4.   

    正则不擅长,但想到一种办法:先遍历每个字符,第一次遇到引号后把接下遇到的空格替换成特殊的字符直至遇到下一个引号,直接把所有成对引号的里的空格都替换成同一种特殊的字符,然后把引号用""replace掉,再用空格split,最后再遍历每个分隔之类的字符串,把特殊字符换回空格.
      

  5.   

    根据上面思路我写了一个例子:string str = "a \"ha ha\" ws ";
                bool start = false;
                for (int i=0;i<str.Length;i++)
                {
                    string one = str.Substring(i, 1);
                    if (one == " ")
                    {
                        if (start)
                        {
                            str = str.Remove(i, 1);
                            str = str.Insert(i, ".");
                        }
                    }
                    if (one == "\"")
                    {
                        start = !start;
                    }
                }
                str = str.Replace("\"", string.Empty);
                string[] strArr= str.Split(new char[] { ' ' });
               
                for (int i = 0; i < strArr.Length; i++)
                {
                    strArr[i] = strArr[i].Replace(".", " ");
                    listBox1.Items.Add(strArr[i]);
                }
      

  6.   

    string s = "aa \"ha ha\" ss";
    MatchCollection mc = Regex.Matches(s, @"""([^""]+)""");s = Regex.Replace(s, @"""[^""]+""", "");
    string[] sp = Regex.Split(s, @"\s+");
    foreach (string str in sp)
    {
        Console.WriteLine(str);
    }
    for (int i = 0; i < mc.Count; i++)
    {
        Console.WriteLine(mc[i].Groups[1]);
    }Console.Read();
      

  7.   

    static void Main(string[] args)
            {
                string strText = "a \"ha ha\" ws \"sdf s d\" bbb";
                Regex r = new Regex("\"[^\"]*\"");            foreach (Match item in r.Matches(strText))
            {
                    strText =  strText.Remove(strText.IndexOf(item.Groups[0].Value), item.Groups[0].Value.Length);
                    Console.WriteLine(item.Groups[0].Value);
            }
                r = new Regex("(\\w*)");
                foreach (Match item in r.Matches(strText))
                {
                    Console.WriteLine(item.Groups[0].Value);
                }
               
                Console.Read();
                
            }只会分两次写。一次的不会写
      

  8.   


    //仅供参考
                String str=@"a ""ha ha"" sds  "" t e s t ""  s""ws";            Regex objRegex = new Regex(@"(?i)(?:""(.+?)"")|(?:([^\s]+))");            MatchCollection objMatches = objRegex.Matches(str);            Response.Write("Result :<br />");            Int32 index = 0;            foreach (Match objMatch in objMatches)
                {
                    Response.Write(String.Format("{0:2} : {1}<br />", ++index, objMatch.Result("$1$2")));
                }
    //结果:
    /*
    Result :
    2 : a
    2 : ha ha
    2 : sds
    2 : t e s t 
    2 : s"ws
    */
      

  9.   

    string pattern = " \"(?<Info>.*)\" ";
    MatchCollection matchCollection = Regex.Matches(text, pattern);
    List<string> resultList = new List<string>();
    int currentIndex = 0;
    foreach(Match match in matchCollection)
    {
        if(match.Index > currentIndex)
        {
            resultList.Add(text.Substring(currentIndex,match.Index-currentIndex+1).Trim());
            currentIndex = match.Index;
        }    resultList.Add(match.Groups["Info"].Value);
    }resultList就是你想要的。你调试一下,看看,我随手敲的,里面应该有一些错误,但基本思路如此。
      

  10.   

    private void button28_Click(object sender, EventArgs e)
            {
                string text = "aa \"ha ha\" ss \"ff ddf sf\" a";
                string pattern = string.Format("{0}(?<Info>.*){1}", "\n", "\n");
                MatchCollection matchCollection = Regex.Matches(text.Replace("\"","\n"), pattern,RegexOptions.Multiline);
                List<string> resultList = new List<string>();
                int currentIndex = 0;
                foreach (Match match in matchCollection)
                {
                    if (match.Index > currentIndex)
                    {
                        resultList.Add(text.Substring(currentIndex, match.Index - currentIndex).Trim());
                        currentIndex = match.Index;
                    }                resultList.Add(match.Groups["Info"].Value);
                    currentIndex += match.Length;
                }            resultList.Add(text.Substring(currentIndex).Trim());            foreach(string result in resultList)
                {
                    MessageBox.Show(result);
                }
            }
      

  11.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Text.RegularExpressions;
    namespace ConsoleApplication1
    {
        class Program
        { 
           
            static void Main(string[] args)
            {
                string s = "a \"ha ha\" ws \"ha ha\" ws";
                s = s + " ";            MatchCollection Ms1 = Regex.Matches(s, "\"(.*)\"", RegexOptions.Multiline| RegexOptions.IgnoreCase);
                foreach (Match m in Ms1)
                {
                    s=s.Replace(m.Groups[1].Value,m.Groups[1].Value.Replace(" ","-"));
                }            
                s=Regex.Replace(s, " ", "\r\n");  
                
                MatchCollection Ms2 = Regex.Matches(s, "(.*)\r\n", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                
                foreach (Match M in Ms2)
                {               
                    Console.WriteLine(M.Groups[1].Value.Replace("-"," "));
                }
                Console.ReadLine();        }
        }
        }