简体   繁体   中英

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 * .

Expected output:

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

This is what I tried,

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

But this gives me below output, replacing on first occurrence

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

You may use a regex with lookahead and lookbehind:

(?<=,)(?=,)

And replace it with "*"

RegEx Demo

Java Code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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