regextextpad

Regex to replace multiple spaces at the end of line with comma or add comma


I'm using Regex and TextPad to clean up and prep sql scripts. I want to replace with comma or add comma:

After spending a few hours researching and iterating I came up with the following which is close to the desired result I want but not perfect. How do I edit this to get the below desired result?

Find what: ' +$|(\n)|$(?![\r\n])'

Replace with: '\1\2,'

I have data that looks like

       dog  *(2 spaces)*
        cat    *(4 spaces)*
        bird*(no space)*
       rodent *(1 space)*
      fish*(no space)*

I want the result to be

    dog,
    cat,
    bird,
    rodent,
    fish,

My result is

        dog,
         cat,
         bird
    ,     rodent,
         fish,

Solution

  • I think you're overcomplicating it. Just match any number of spaces at the end of the line, and replace with comma.

    Find: \s*$ Replace with: ,