12

In Notepad++, I have many lines in the format:

abc.xyz

where "abc" might have space or - or numbers.

How do I delete lines without "."? So if a line does not have any ".", I want it removed completely, not even leave an empty line.

0

2 Answers 2

39

How to delete lines without .?

  • Menu "Search" > "Replace" (or Ctrl + H)

  • Set "Find what" to ^[^\.]*\r\n

    • You can replace \r\n with different EOL (End of Line) regular expressions depending on the EOLs in your file (see "I have a different EOL in my file, what can I do?" and "I don't care what EOLs my file uses, what can I do?" below).
  • Clear "Replace with"

  • Enable "Regular expression"

  • Click "Replace All"

    Image

Notes:

  • The above assumes that the last line in the file has a trailing EOL.

  • The above also assumes you are editing a text file with Windows EOLs, \r\n.

Before:

abc.xyz
abcdef
abc 123.xyz
abc 123def

After:

abc.xyz
abc 123.xyz

I have a different EOL in my file, what can I do?

  • The Windows EOL is \r\n.

  • If you are using files with a different EOL you can convert them to Windows EOLs using Menu "Edit" > "EOL Conversion".

  • If you aren't working with Windows EOLs, and you don't wish to convert them, use the following instead:

    • Use \n instead of \r\n for Unix/OS X EOLs

    • Use \r instead of \r\n for Mac OS (up to version 9) EOLs


I don't know or care what EOLs my file uses, what can I do?

  • You can use \R or (?:\r\n?|\n) or (?:\r?\n?) instead of \r\n. This gets around any issue with the EOLs actually used in the file.

  • You can also use (?:\r?\n?|$). This expression will work if there is no EOL in the last line of the file.


Further reading

14
  • 5
    You can use \R to match any linebreak.
    – user694733
    Feb 21, 2016 at 21:41
  • @user694733 Please explain? \r does not match the EOL on Windows. If you remove the \n you are left with blank lines. OP Stated "I want it removed completely, not even leave an empty line."
    – DavidPostill
    Feb 21, 2016 at 21:44
  • Uppercase R. N++ uses PCRE which has this as a special character class if my memory serves me right.
    – user694733
    Feb 21, 2016 at 21:47
  • @user694733 Ah. OK. Tested it and it works.
    – DavidPostill
    Feb 21, 2016 at 21:52
  • regex101.com is a better site for learning regex than regexexr
    – cat
    Feb 21, 2016 at 23:39
14

The accepted answer is correct, but you won't always have regex at ready. So I present a simpler solution.

  1. Ctrl+F – click on tab Mark
  2. Set "Find what" to .
  3. Check option "Bookmark line" (courtesy of Wrass)
  4. Search mode = Normal
  5. Click Mark All -> You should see a blue circle next to the bookmarked line numbers.
  6. Then navigate to menu Search -> Bookmark -> Remove Unmarked Lines.
4
  • This is a nice simple alternative, however you may want to mention that you should also check the 'Bookmark line' option in the Mark dialog. Failure to do so will result in all lines being removed.
    – Wrass
    Feb 22, 2016 at 12:31
  • Good point, my notepad remembered it from the last time, so I forgot about that.
    – Zikato
    Feb 22, 2016 at 12:53
  • I use regex all the time, but I would still use this method for its simplicity. Disclaimer: I also use the various bookmark functions in Notepad++ a lot.
    – JPhi1618
    Feb 22, 2016 at 17:09
  • Since you were unhappy with your formatting, I've submitted an edit that I think you'll find more pleasant. Feel free to reject it or roll back if you disagree.
    – jpmc26
    Feb 23, 2016 at 0:36

Not the answer you're looking for? Browse other questions tagged .