简体   繁体   English

在C#中使用'分割字符串

[英]Split string with ' in C#

How to split this string 如何分割这个字串

1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1

and get this string array as result 并得到这个字符串数组作为结果

1014
'0,1031,1032,1034,1035,1036'
0
0
1
1
0
1
0
-1
1

in C# 在C#中

I believe that this regex should give you what you are looking for: 我相信这个正则表达式应该为您提供所需的东西:

('(?:[^']|'')*'|[^',\r\n]*)(,|\r\n?|\n)?

http://regexr.com?2vib4 http://regexr.com?2vib4

EDIT: 编辑:

Quick code snippet on how it might work: 有关其工作方式的快速代码段:

 var rx = new Regex("('(?:[^']|'')*'|[^',\r\n]*)(,|\r\n?|\n)?");
 var text= "1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1";

 var matches = rx.Matches(text);

 foreach (Match match in matches)
 {
     System.Console.WriteLine(match.Groups[1].ToString());
 }

This code is not pretty at all, but it works. 这段代码一点也不漂亮,但是可以用。 :) (Does not work with multiple "strings" within the string.) :)(不适用于字符串中的多个“字符串”。)

void Main()
{
    string stuff = "1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1";
    List<string> newStuff = new List<string>();

    var extract = stuff.Substring(stuff.IndexOf('\''), stuff.IndexOf('\'', stuff.IndexOf('\'') + 1) - stuff.IndexOf('\'') + 1);
    var oldExtract = extract;
    extract = extract.Replace(',',';');
    stuff = stuff.Replace(oldExtract, extract);
    newStuff.AddRange(stuff.Split(new[] {','}));
    var newList = newStuff;
    for(var i = 0; i < newList.Count; i++)
        newList[i] = newList[i].Replace(';',',');
    // And newList will be in the format you specified, but in a list..
}

try this, 尝试这个,

string line ="1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1" ;
var values = Regex.Matches(line, "(?:'(?<m>[^']*)')|(?<m>[^,]+)");
foreach (Match value in values) {
  Console.WriteLine(value.Groups["m"].Value);
}

首先在' (单引号)上分割字符串,然后使用comma (,)。

You don't need a parser, you don't need Regex. 您不需要解析器,也不需要正则表达式。 Here's a pretty simple version that works perfectly: 这是一个非常简单的版本,可以完美运行:

var splits = input
            .Split('\'')
            .SelectMany(
                (s,i) => (i%2==0) 
                ? s.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries) 
                : new[]{ "'" + s + "'"}
            );

This is exactly what @AVD + @Rawling described ... Split on ' , and split only "even" results, then combine. 这正是@AVD + @Rawling所描述的...在'分割,只分割“偶数”结果,然后合并。

using System;
using System.IO;
using Microsoft.VisualBasic.FileIO; //Microsoft.VisualBasic.dll

public class Sample {
    static void Main(){
        string data = "1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1";
        string[] fields = null;
        data = data.Replace('\'', '"');
        using(var csvReader = new TextFieldParser(new StringReader(data))){
            csvReader.SetDelimiters(new string[] {","});
            csvReader.HasFieldsEnclosedInQuotes = true;
            fields = csvReader.ReadFields();
        }
        foreach(var item in fields){
            Console.WriteLine("{0}",item);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM