我有一个输入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"
我只想将字段分隔符从逗号(,)更改为脱字符号(^),因此在从输入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中处理所有这些情况,或者也可以在多个replaceAll中处理
答案 0 :(得分:1)
我想这很适合您;
text = text.replaceAll("\",\"(?!\")", "\"^\"");
\“,\”(?!\“)这部分表示如果“ \””后面没有“ \””,则以下内容将与“ \””匹配。