81

Using Notepad++, how do I remove all lines starting with # or ;?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Wasim A.
  • 9,660
  • 22
  • 90
  • 120

6 Answers6

139

Find:

^[#;].*

Replace with nothing. The ^ indicates the start of a line, the [#;] is a character class to match either # or ;, and .* matches anything else in the line.

In versions of Notepad++ before 6.0, you won't be able to actually remove the lines due to a limitation in its regex engine; the replacement results in blank lines for each line matched. In other words, this:

# foo
; bar
statement;

Will turn into:



statement;

However, the replacement will work in Notepad++ 6.0 if you add \r, \n or \r\n to the end of the pattern, depending on which line ending your file is using, resulting in:

statement;
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 10
    You can replace blank lines by using Search Mode "Extended" and finding \r\n\r\n. If you leave "replace with" blank, you'll remove the blank lines. Be careful, as this will remove other blank lines that were not created by your first replace. – PaulF Mar 03 '11 at 22:05
  • 5
    As of notepad++ 6, with full PCRE regex support, searching for ^(#|;).*\r\n and replacing with blank (with the regex option enabled of course) works perfectly – Tao Apr 13 '12 at 10:38
  • @Tao: Thanks for the reminder - I forgot to update all my Notepad++ answers with the 6.0 support! I'll get to that soon... – BoltClock Apr 13 '12 at 10:42
  • np, I just picked on you because this answer was in the top three results when I searched for something related to "notepad++ regular expressions" in google (although I can't reproduce the search now). I stumbled across this PCRE suport today by accident, made my day. – Tao Apr 13 '12 at 11:06
  • In current versions of Notepad++ (don't know since when), \R matches \r\n, \r, and \n. – Rainald62 Dec 21 '20 at 15:43
10

As others have noted, in Notepad++ 6.0 and later, it is possible to use the "Replace" feature to delete all lines that begin with ";" or "#".

Tao provides a regular expression that serves as a starting point, but it does not account for white-space that may exist before the ";" or "#" character on a given line. For example, lines that begin with ";" or "#" but are "tabbed-in" will not be deleted when using Tao's regular expression, ^(#|;).*\r\n.

Tao's regular expression does not account for the caveat mentioned in BoltClock's answer, either: variances in newline characters across systems.

An improvement is to use ^(\s)*(#|;).*(\r\n|\r|\n)?, which accounts for leading white-space and the newline character variances. Also, the trailing ? handles cases in which the last line of the file begins with # or ;, but does not end with a newline.

For the curious, it is possible to discern which type of newline character is used in a given document (and more than one type may be used): View -> Show Symbol -> Show End of Line.

Ben Johnson
  • 2,507
  • 3
  • 29
  • 29
  • The regex you suggest seems to fail in locating the last line of the document (if it is a relevant line). – maja Jun 29 '16 at 11:21
  • 1
    Nice catch, @maja! Try appending a `?` to the end, like this: `^(\s)*(#|;).*(\r\n|\r|\n)?`. I will update the original answer accordingly. Thank you! – Ben Johnson Jun 29 '16 at 14:41
8

Its possible, but not directly.

In short, go to the search, use your regex, check "mark line" and click "Find all". It results in bookmarks for all those lines.

In the search menu there is a point "delete bookmarked lines" voila.

I found the answer here (the correct answer is the second one, not the accepted!): How to delete specific lines on Notepad++?

Community
  • 1
  • 1
stema
  • 90,351
  • 20
  • 107
  • 135
4

Maybe you should try

^[#;].*$

^ matches the beggining, $ the end.

António Almeida
  • 9,620
  • 8
  • 59
  • 66
Lundberg
  • 404
  • 3
  • 4
  • Will not work, [see scintilla](http://www.scintilla.org/SciTERegEx.html). The end-of-line chars are stripped of. – stema Mar 03 '11 at 22:26
1

In Notepad++, you can use the Mark tab in the Find dialogue to Bookmark all lines matching your query which can be regex or normal (wildcard).

Then use Search > Bookmark > Remove Bookmarked Lines.

NialF
  • 21
  • 2
  • The same answer has already been written by **stema** but your answer here has much less information. Whilst adding new answers to old questions can be good they are only useful if they provide new information. Answers that just repeat other answers have no value, they are just clutter. – AdrianHHH Aug 28 '15 at 10:00
0
.*?[#;].*?\r\n

replace to nothing