Search code examples
regexlibreofficeopenoffice.org

Remove any blank or whitespaced lines using regex


I am trying to find and remove any blank or whitespaced lines in OpenOffice document using regex.

Currently I can do it in two steps:

  1. Search for ^$ and replace with nothing.

    This will remove any empty lines.

  2. Search for ^\s*$ and replace with nothing.

    This will remove any lines which contains only spaces or tabs.

    Important note: From my point of view, this 2nd version should also remove any empty lines (as 1st version), but actually it doesn't.

So, there are actually two questions.

  • For what reason second regex matches only lines with spaces and tabs, but don't match empty lines?

  • Is there way to combine first and second version to achieve desired result in one step? Here what I tried: ^$|^\s*$ and (^|^\s*)$. But it doesn't work. It matches only whitespaced lines, but not empty ones.

Text for test:

Just for example, I changed spaces to dots
and tabs to dashes.


aa

..........................

-------------------

aaaaaaaaaaaaaa

aaaaaaaaaaaaaaaaaaa

Desired result:

aa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaa

Solution

  • AltSearch can do this as a single step using a Batch script. In the AltSearch dialog, click on Batch >>. Then Edit the file and paste the following code at the end.

    [Name] Remove any blank or whitespaced lines
    
      ; Remove any lines which contains only spaces or tabs.
      [Find]^\s*$
      [Replace]
      [Parameters] MsgOff Regular 
      [Command] ReplaceAll  
    
      ; Remove any empty lines.
      [Find]^$
      [Replace]
      [Parameters] MsgOff Regular 
      [Command] ReplaceAll
    
    [End]  
    

    Now, save the text file and click Refresh. Finally, click on Remove any blank or whitespaced lines and press Execute.

    This produces the desired result and shows a single dialog:

    Batch   'Remove any blank or whitespaced lines'   is ended. 
    
    10  replacements have been done.