【问题标题】:Regular expression for splitting a comma with the string用字符串分割逗号的正则表达式
【发布时间】:2020-01-14 09:21:49
【问题描述】:

我不得不根据逗号分割字符串数据。

这是excel数据:- Please find the excel data

string strCurrentLine="\"喜马拉雅盐身体磨砂膏,含 Majestic Pure 荔枝精油,全天然磨砂膏,去角质和滋润肌肤,12 盎司\",SKU_27,\"Tombow 双刷笔艺术标记,肖像,6- Pack\",SKU_27,My Shopify Store 1,Valid,NonInventory"。

Regex CSVParser = new Regex(",(?=(?:[^\"]\"[^\"]\")(?![^\"] \"))"); string[] lstColumnValues = CSVParser.Split(strCurrentLine);

I have attached the image.The problem is I used the Regex to split the string with comma but i need the ouptut just like SKU_27 because string[0] and string2 包含正反斜杠。我需要输出字符串1 并删除正反斜杠。

【问题讨论】:

  • 欢迎来到 StackOverflow!请不要将文本数据附加为图像,没有人会输入它。还请告诉我们更多关于您以前解决问题的尝试,例如。您可以粘贴代码片段等。

标签: c#-4.0


【解决方案1】:

该文件似乎是 CVA 文件。为了使 CVA 格式正确,它将使用引号 "" 来包装包含逗号的字符串,例如

身份证、姓名、日期
1,“一些文本,包括逗号”,2020/01/01

简单地用逗号分割字符串,你会得到带有双引号的第二列。

【讨论】:

    【解决方案2】:

    我不确定您是在问如何删除lstColumnValues[0]lstColumnValues[2] 中的双引号,还是将它们添加到lstColumnValues[1]

    要删除双引号,只需使用Replace

    string myString = lstColumnValues[0].Replace("\"", "");
    

    如果您需要添加它们:

    string myString = $"\"{lstColumnValues[1]}\"";
    

    【讨论】:

      猜你喜欢
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多