fvaliquette
fvaliquette

Reputation: 87

Capture numbers and strings from comma separatated value file

I have a file which contains strings that begins and end with double quotes. Each string can contain a comma. Numbers do not begin or end with double quotes. Each integer and string are separated by a comma. It is also possible to have a null value.

I would like to make groups witch each string and number. I am trying to capture each group one at a time.

I have created this regex, it works for every case unless there is a comma in the string:

/(?:"?([^"]*)"?,){2}/U

If I remove the ungreedy operator, the regex works for every case except for null values.

Here is an example of the log file:

196778,"df,fdfsdf","4.4","ds-sdads231-33","mmh",1,,,,,,,023,1,"20150,62519535TY"

https://regex101.com/r/kO5wM4/3

Upvotes: 1

Views: 53

Answers (1)

anubhava
anubhava

Reputation: 785601

You can use this regex:

(?:(?:"([^"]*)"|([^,]*))(?:,|$)){2}

RegEx Demo

Upvotes: 1

Related Questions