-3

I have a CSV string that I want to split on commas, but only if the comma isn't preceded by a specific word somewhere else before it in the string.

Below is an example of the input.

1,2,3,Test Message,JSON={ "book": { "title": "Hello, there, world" } }

In the above case, any commas after the word 'JSON=' should not cause the string to split, so I would be hoping for the following split.

1
2
3
Test Message
JSON={ "book": { "title": "Hello, there, world" } }

Ideally, I'd like to use Regex.Split to split the string and I think I need to use a negative look behind, but I'm not sure of the syntax required to achieve this.

Any help would be greatly appreciated.

Jack
  • 33
  • 1
  • 8
  • 1
    It might easier to match the portion up the stop word, assuming the stop word portion is at the end and then split the front portion. – Lee Sep 30 '21 at 16:12
  • In the general case, CSV parsing requires stateful processing of quoted strings. You can't just throw `Split()` at it, not even a regex-capable `Split()`. – Ben Voigt Sep 30 '21 at 16:28
  • Also note that while this data is valid CSV, it is not a valid CSV encoding of JSON. Proper CSV parsing will consume the quotes leaving none in the output for the JSON parser to find. – Ben Voigt Sep 30 '21 at 16:30

1 Answers1

0

You can use below regex to parse JSON in CSV mentioned here

C# code -

var csvContent = "1,2,3,Test Message,JSON={ \"book\": { \"title\": \"Hello, there, world\" } }";
var split = Regex.Split(csvContent, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");

JS demo -

const csv = '1,2,3,Test Message,JSON={ "book": { "title": "Hello, there, world" } }';

console.log(csv.split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/));
Nikhil Patil
  • 2,480
  • 1
  • 7
  • 20