开发者

Inserting characters before whatever is on a line, for many lines

开发者 https://www.devze.com 2023-01-05 08:05 出处:网络
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 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 acc开发者_Go百科ess 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?


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消