I would like to have a CSV parser. However, I would like to have the following: the regex must check, that the CSV ends with a comma
, (otherwise the regex must not consider the CSV as valid)! If CSV is OK, then I will extract the text between
hello,world,end, //OK. CSV ends with ",". There are 3 matches: 'hello' 'world' 'end'
aa,bb,cc //NOT ok. CSV doesn't end with ",". No matches.
I have a question to the regex suggested by @Denomales. If the CSV contains quoted parts, there is always a match that contains only a quotation mark. Is it possible to avoid it?
Best How To :
Speaking about the regex suggested by @Denomales, there can be something we can do about it.
To make sure we only match a string with a comma at the end, you can add a positive look-ahead at the beginning of the pattern (marked with
And if you do not want to capture the entry-delimiting mark (the quotes), you can just remove the
(?=[^"]|(")?) look-ahead (marked with
I see you have posted 2 answers to the above mentioned thread. Your regex is almost all you need, just add the look-ahead and a means to skip escaped entities:
See Demo 2