views:

277

answers:

3

Hello all,

I've got some configuration files with hundreds of lines of data each.

I'd like to begin each line (after the carriage return) with a pound sign ('#') unless it's already beginning with a pound sign. I don't want to do a find+replace because I've got other commented parts of the file that aren't the beginning of a line.

So for example, this:

Some data #some comment
Some more data #some other comment
Even more data
#some line comment

would turn into:

#Some data #some comment
#Some more data #some other comment
#Even more data
#some line comment
+2  A: 

Use the Search -> Replace feature, but put it in Regular Expression mode and match on line beginnings:

Find What:    ^([^#].*)
Replace With: #\1

This will match a line and everything in it, put that in \1, and then replace it with a # followed by what was put in \1 (the entire line).

Amber
Thanks!This works when I click "Replace" but it yells at me when I click "Replace All" saying: "The regular expression to search is formed badly". Any thoughts?
mheathershaw
Hm. I just tried it myself and Replace All worked fine. What version of NPP are you using?
Amber
It's version 5.6.1.
mheathershaw
Works great! Thank you!!
mheathershaw
+1  A: 

Try find and replace with this regex (in regex mode):

^([^#])

and replace all with #\1. (Edited with Dav's feedback - thanks!)

Skilldrick
Damn, just not quick enough :)
Skilldrick
Only one problem with your regex - it'd replace the first character on the line, instead of just prepending a `#`. You'd need to put a capture group around that `[^#]` and then add the captured character to the replace term.
Amber
Or you could do a forward lookahead.
Paul Lammertsma
Notepad++ won't do advanced regex like lookaheads...
Skilldrick
A: 
  1. Change the Language to Perl (Alt, L, P, DOWN)
  2. Select All (CTRL-A)
  3. Block Comment (CTRL-Q)
  4. Change Language back

Edit: never mind. Won't ignore already commented lines. Oh well.

John at CashCommons