简体   繁体   English

正则表达式替换 java 中 csv 字符串中的空值

[英]Regex to replace empty value in a csv string in java

I have a string with value我有一个带值的字符串

 "ABC","ONE", "Hello",,,"2019"

I want to replace two consecutively empty values in a CSV file with * .我想用*替换 CSV 文件中的两个连续空值。

Expected output:预期 output:

 "ABC","ONE", "Hello","*","*","2019"

This is what I tried,这是我试过的,

line.replaceAll(",,","\"*\"");

But this gives me below output, replacing on first occurrence但这给了我低于 output,第一次出现替换

"ABC","ONE", "Hello","*","2019"

You may use a regex with lookahead and lookbehind:您可以使用带有前瞻和后视的正则表达式:

(?<=,)(?=,)

And replace it with "*"并将其替换为"*"

RegEx Demo正则表达式演示

Java Code: Java 代码:

repl = str.replaceAll("(?<=,)(?=,)", "\"*\"");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM