Pergunta

I've been wrestling for the last days to create a regular expression that would match the quotes (if there are any) in the filed values of a CSV file string.

I would want to match only the quotes or at least the whole field value. e.g. : "", " ,a "test" in a CSV string like this:

"ID","iManufacturer","iMPartNumber","iSerialNumber","iSimCategory"
"1","""","230-132-111AA"," " ","a "test""

The reason is to escape the quotes if they appear inside values so that the CSV file could be correctly parsed.

Without lookbehinds in JavaScript I find it hard to find the solution. So far I've come up with this:

(?:[^,\r\n\t\f]*)["]+(?:[^,\r\n\t\f]*)["]+(?:[^,\r\n\t\f]*)(?=",|"\s|"\S)

It almost matches the strings that I need (it also matches the start of the value quote) : """, " " ,"a "test". It does not match if on the last row, the last value contains quotes.

Any help? Or maybe other way of doing it?

Foi útil?

Solução

You could use this regular expression:

"((?:[^,"\n]*?"[^,"\n]*?)+)"

You can find an in detail explanation here.

Hope that helps.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top