Search code examples
regexnotepad++

Notepad++ replace blank space with 0 in csv


I have one CSV file with data like below.

ABC,XYZ,LMN
0,,2.3
3.4,3,5
,0,
1.3,,4.5

Is there any way to replace the empty column with a 0 value using Regex?

EDIT: Tried Tim Biegeleisen solution like below

Find:    (^|,)(,|$)
Replace: ${1}0${2}

But it doesn't work in below case. If I have CSV data like below

ABC,XYZ,LMN
0,,2.3
3.4,3,5
,,0 // here it doesn't work
1.3,,

Solution

  • You can use

    Find:    (?<![^,\r\n])(?=,|$)
    (or)     (?:,|^)\K(?=,|$)
    Replace: 0
    

    Details:

    • (?<![^,\r\n]) - a negative lookbehind that fails the match if there is no ,, CR or LF char immediately on the left (basically, it is roughly (?<=,|^), a comma or start of a line must occur immediately to the left of the current location)
    • (?:,|^)\K - matches a comma or start of a line and \K removes the comma (if matched) from the match value (it is a kind of a lookbehind alternative)
    • (?=,|$) - a positive lookahead that requires either , or end of a line immediately to the right of the current location.

    See the regex demo:

    enter image description here