简体   繁体   English

C#文本拆分逻辑逗号分隔符和字符串标识符

[英]C# text split logic comma separator and string identifier

I need to split text by "comma seperater" ... and "string identifier" 我需要用“逗号分隔 符”“字符串标识符”分割文本

input "dtl.txt" 输入 “ dtl.txt”

AWD_CODE,AWD_NAME,AWD_TYPE,ADF_REF,FLG_SUM,FLG
DMM,PETCH,01,REF 2/2015,,
TRR,TUCTH,01,REF 2/2015,WD_TRK,F
TGC,DHYTH,02,REF 3/2015,"WD_TRK,WD_TRI",F

operation 操作

  static void Main(string[] args)
        {
            string[] lines = System.IO.File.ReadAllLines(@"D://dtl.txt");

            List<string[]> param = new List<string[]>();

            foreach(string line in lines)
            {
                param.Add(line.Split(','));
            }

            var x = param; // for debug
        }

output (get) 输出 (获取)

array : 
[0] : "AWD_CODE","AWD_NAME","AWD_TYPE","ADF_REF","FLG_SUM","FLG"
[1] : "DMM","PETCH","01","REF 2/2015","",""
[2] : "TRR","TUCTH","01","REF 2/2015","WD_TRK","F"
[3] : "TGC","DHYTH","02","REF 3/2015","\"WD_TRK","WD_TRI\"","F"

output (need) 输出 (需要)

array : 
[0] : "AWD_CODE","AWD_NAME","AWD_TYPE","ADF_REF","FLG_SUM","FLG"
[1] : "DMM","PETCH","01","REF 2/2015","",""
[2] : "TRR","TUCTH","01","REF 2/2015","WD_TRK","F"
[3] : "TGC","DHYTH","02","REF 3/2015","WD_TRK,WD_TRI","F"

"WD_TRK,WD_TRI" yes that code split it too. “ WD_TRK,WD_TRI”是的,代码也将其拆分。

But i not need , can anyone help solve this problem ? 但是我不需要,任何人都可以帮助解决这个问题吗?

This is the situation where TextFieldParser in the Microsoft.VisualBasic.FileIO library is best fit. 在这种情况下, Microsoft.VisualBasic.FileIO库中的TextFieldParser最适合。

using Microsoft.VisualBasic.FileIO; //add this

static void Main(string[] args)
{
    string text = System.IO.File.ReadAllText(@"D://dtl.txt"); //note this

    List<string[]> param = new List<string[]>();
    string[] words; //add intermediary reference

    using (TextFieldParser parser = new TextFieldParser(new StringReader(text))) {
        parser.Delimiters = new string[] { "," }; //the parameter must be comma
        parser.HasFieldsEnclosedInQuotes = true;
        while ((words = parser.ReadFields()) != null)
            param.Add(words);
    }

    var x = param; // for debug
}

And you shall get what you need. 您将得到所需的东西。 Read this . 阅读

Output: 输出:

array : 
[0] : "AWD_CODE","AWD_NAME","AWD_TYPE","ADF_REF","FLG_SUM","FLG"
[1] : "DMM","PETCH","01","REF 2/2015","",""
[2] : "TRR","TUCTH","01","REF 2/2015","WD_TRK","F"
[3] : "TGC","DHYTH","02","REF 3/2015","WD_TRK,WD_TRI","F"

To use it, you need to include Microsoft.VisualBasic in your reference. 若要使用它,您需要在参考中包括Microsoft.VisualBasic

Unless you use a specialized CSV library in this particular case (highly recommended), then you will need to write a regular expression. 除非在这种情况下(特别推荐)使用专门的CSV库,否则您将需要编写正则表达式。 See C#, regular expressions : how to parse comma-separated values, where some values might be quoted strings themselves containing commas for a similar question. 参见C#,正则表达式:如何解析逗号分隔的值,其中一些值可能被引号包括相似问题的字符串本身包含逗号 The regular expression given there was 那里给出的正则表达式是

"[^"\\r\\n]*"|'[^'\\r\\n]*'|[^,\\r\\n]*

with this code to execute it: 用以下代码执行它:

Regex regexObj = new Regex(@"""[^""\r\n]*""|'[^'\r\n]*'|[^,\r\n]*");
Match matchResults = regexObj.Match(input);
while (matchResults.Success) 
{
    Console.WriteLine(matchResults.Value);
    matchResults = matchResults.NextMatch();
}

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

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