Split string by commas ignoring any punctuation marks (including ',') in quotation marks

前端 未结 6 1719
甜味超标
甜味超标 2021-01-22 06:10

How can I split string (from a textbox) by commas excluding those in double quotation marks (without getting rid of the quotation marks), along with other poss

6条回答
  •  一个人的身影
    2021-01-22 06:26

    Another Regex solution:

    private static IEnumerable Parse(string input)
    {
      // if used frequently, should be instantiated with Compiled option
      Regex regex = new Regex(@"(?<=^|,\s)(\""(?:[^\""]|\""\"")*\""|[^,\s]*)");
    
      return regex.Matches(inputData).Where(m => m.Success);
    }
    

提交回复
热议问题