当前位置: 代码迷 >> .NET Framework >> 分割字符串正则表达式,该怎么解决
  详细解决方案

分割字符串正则表达式,该怎么解决

热度:114   发布时间:2016-05-02 00:33:49.0
分割字符串正则表达式
有这样类似的字符串 :abc and deandf and "hij and abc and def or dcd" or "abc"

用 and|or 进行分割,但""双引号之间的 and|or 不作为分割符
结果如下
abc
deandf 
"hij and abc and def or dcd" 
"abc"



------解决方案--------------------
try...

C# code
            string test = "abc and deandf and \"hij and abc and def or dcd\" or \"abc\"";            Regex reg = new Regex(@"(?s)\b(?:and|or)\b(?=(?:(?:[^""]*""){2})*[^""]*$)");            string[] result = reg.Split(test);            foreach (string s in result)            {                richTextBox2.Text += s + "\n";            }