У меня есть вход CSV , в котором данные заключены в двойные кавычки, а разделитель полей - запятая (,). Как показано ниже, 3 столбца и 1 строка:
"Id","Description","LastModifiedDate","Quantity"
"101","this is a test message - "","" how are you, where are you from","2018-01-13","15.0"
"102","this is line break msg , "2019-01-01","13.0"
where data goes to next line"
Я хочу изменить только разделитель полей с запятой (,) на каретку (^), поэтому при чтении строки из Input CSV я написал line.replace ("\", \ "", "\" ^ \ "") ;
Получилось ниже фактический результат :
"Id"^"Description"^"LastModifiedDate"
"101"^"this is a test message - ""^"" how are you, where are you from"^"2018-01-13"^"15.0"
"102"^"this is line break msg ^ "2019-01-01"^"13.0"
where data goes to next line"
Проблема заключается в использовании вышеуказанного кода замены, он заменяет все запятые на каретки, которые я не хочу. Ожидаемый результат должен быть таким, как показано ниже:
"Id"^"Description"^"LastModifiedDate"
"101"^"this is a test message - "","" how are you, where are you from"^"2018-01-13"^"15.0"
"102"^"this is line break msg ^ "2019-01-01"^"13.0"
where data goes to next line"
Насколько я знаю, это можно сделать с помощью регулярных выражений Java, но, к сожалению, я не очень хорош в использовании регулярных выражений, поэтому любая помощь будет очень полезна.
Обновление
Regex1 : replaceAll("\",\"(?!\"\")", "\"^\"");
Example1,
"Id","Description","LastModifiedDate","Quantity" -- header
"101","hello-this,is test data"",""testing","2018-10-01","\" -- input row1
"101"^"hello-this,is test data""^""testing"^"2018-10-01"^"\" -- post Regex1
"101"^"hello-this,is test data"",""testing"^"2018-10-01"^"\" -- expected
In first row if data contains "","" it still gets replaced to ""^""
Example2,
"Id","Description","LastModifiedDate","Quantity" -- header
"102","""text in double quotes""","13.2" -- input row2
"102","""text in double quotes"""^"13.2" -- post with only Regex1
"102"^""text in double quotes""^"13.2" -- expected result
So I tried one more regex after regex1 for second row scenario
Regex 2: replaceAll(",\"\"\"(?!\"\")", "^\"\"");
regex2 along with regex1 partially worked but still, the row1 issue is not getting resolved.
Могут ли все эти сценарии обрабатываться в 1 replaceAll или в множественной замене все также будет делать