java - Java如何仅更改字段分隔符而不更改实际数据
问题描述
我有一个输入 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 中处理吗,或者多个 replaceAll 也可以
解决方案
我想这适合你;
text = text.replaceAll("\",\"(?!\")", "\"^\"");
\",\"(?!\") 这部分表示如果 "\"" 后面没有 "\"",则后面将匹配 "\""。
推荐阅读
- c - 联合内部的结构和具有结构指针的联合。如何访问它们?
- flutter - Flutter:StaggeredGridView 中的 itemBuilder 索引顺序错误
- javascript - 等待`dispatch`以隐藏带有`setState`的模态框
- sql - sql 多个日期按聚合分组
- mysql - mysql无法在视图中显示重复的条目
- python - 生成所有可能的组合,其中连续对的最大差异为 1000
- java - 在 ArrayList 上按降序实现 QuickSort
- flutter - 如何进行复杂查询以计算与 Firestore 上的查询匹配的嵌套对象?
- python - Python无法录制音频
- python - 加入后条件下 SQL 更新的 Pandas 等效项