I work in Notepad ++.

Suppose I want to select only the third line in order to replace it with another in many documents using the Заменить в файлах function Заменить в файлах .

Tried to use

 \A^(.*){2}.*$ 

Unfortunately, this expression finds not only the third, but also the subsequent lines in a multi-line document.

PS: This question is in English.

  • Give an example of the input and output you want to get. Your question is not completely transparent in understanding. - Firepro
  • Isn't it easier to use Seek, which is in almost all programming languages ​​and move to line 3, and then do anything with it? - iluxa1810
  • / ^ [^ \ n] * \ n [^ / n] * \ n ([^ \ n] *) \ n / \ 1 / - rjhdby
  • I tried this \ A (^. *?) {2} ^. * $ But doesn’t work I use Notepad ++ - Just Me
  • practically I want to fit a certain line - Just Me

1 answer 1

The problem is that in Notepad ++, search using regular expressions works cyclically when using mass (global) replacement. Replacing the 3rd row, the index is located at the beginning again, and the 6th row becomes the 3rd, and so on.

In this case, you need to find a match equal to the entire document. You can use

 \A((?:.*\R){2}).*((?s:.*))\z 

As a replacement pattern, you can use $1Моя_новая_строка$2 .

Details:

  • \A - the beginning of the document
  • ((?:.*\R){2}) - Exciting group (submask) No. 1, which searches for 2 sequences of zero and more characters other than the newline character, followed by the newline character (the first two lines)
  • .* - 0 or more characters other than a newline character (what will be replaced)
  • ((?s:.*)) - 2nd fascinating group that finds any 0 or more characters before ....
  • \z - the end of the document.

$1 - back link to the content of the 1st exciting subplay.