views:

42

answers:

3

I have been looking at regular expressions to try and do this, but the most I can do is find the start of a line with ^, but not replace it.

I can then find the first characters on a line to replace, but can not do it in such a way with keeping it intact.

Unfortunately I don´t have access to a tool like cut since I am on a windows machine...so is there any way to do what I want with just regexp?

+1  A: 

Did you try replacing the regular expression ^ with the text you want to put at the start of each line? Also you should use the multiline option (also called m in some regex dialects) if you want ^ to match the start of every line in your input rather than just the first.

string s = "test test\ntest2 test2";
s = Regex.Replace(s, "^", "foo", RegexOptions.Multiline);
Console.WriteLine(s);

Result:

footest test
footest2 test2
Mark Byers
This does not work in notetab++, which is what I was using...should I try a different texteditor that supports the example you gave?
Jacob
I think you misunderstood the question- I believe the lines do not start with a "^", at least not all of them.
PeterK
oh nm, it did work....@peter, i am trying to replace the start of a line with new text, and marks example works fine without the quotation marks in notetab++
Jacob
Oh, okay. The important thing is you achieved what you wanted! :)
PeterK
+1  A: 

Use notepad++. It offers a way to record an sequence of actions which then can be repeated for all lines in the file.

PeterK
A: 

I used to program on the mainframe and got used to SPF panels. I was thrilled to find a Windows version of the same editor at Command Technology. Makes problems like this drop-dead simple. You can use expressions to exclude or include lines, then apply transforms on just the excluded or included lines and do so inside of column boundaries. You can even take the contents of one set of lines and overlay the contents of another set of lines entirely or within column boundaries which makes it very easy to generate mass assignments of values to variables and similar tasks. I use Notepad++ for most stuff but keep a copy of SPFSE around for special-purpose editing like this. It's not cheap but once you figure out how to use it, it pays for itself in time saved.

T.Rob