Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

  • 1 or more spaces at the end of each line
  • at end of each line or the last line (i.e. "end of file")

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])'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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: ,

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading