English 中文(简体)
Begin each new line with a character (unless it already begins with that character)
原标题:

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
最佳回答

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).

问题回答

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

^([^#])

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

  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.





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签