Archive

Posts Tagged ‘regex pattern’

How to Remove Empty Lines in Text using Visual Studio

May 6th, 2008 Jacob Dybala 2 comments

You can remove empty lines in text using Microsoft Visual Studio’s code editor by using the replace operation with regular expressions. Regular expressions are a very powerful tool that can be used to search for almost anything in text.

  1. Invoke the Replace dialog box (click Ctrl-G)
  2. Make sure "Use Regular Expressions" is checked
  3. In Find type in ^$\nHere’s what this text string means:^ (caret) – Matches at the start of the string the regex pattern is applied to. Matches a position rather than a character. Most regex flavors have an option to make the caret match after line breaks (i.e. at the start of a line in a file) as well.

    $ (dollar) – Matches at the end of the string the regex pattern is applied to. Matches a position rather than a character. Most regex flavors have an option to make the dollar match before line breaks (i.e. at the end of a line in a file) as well. Also matches before the very last line break if the string ends with a line break.

    \n – Matches an LF (line feed) character. Can be used in character classes.

  4. Leave Replace field blank.
  5. Click "Replace All".

Voilà! All empty lines will be deleted.