How to insert text into the second line with RegEx in Notepad ++ ?
-
@guy038
Thank you!
Each file will begin with non-empty line. -
@Terry-R said in How to insert text into the second line with RegEx in Notepad ++ ?:
\1New Line 2\r\n\3
Hi Terry,
This code works perfectly:
Search
(?-s)^((.+)?\R)(?s)(.+)*
Replace
Replace With:\1New Line 2\r\n\3My notes:
There is a 1 before the intended insert text.P.s I do not need to open my files first.
-
Hi Terry,
I tried the code it works fine.
The replacing code is
\1New Line 2\r\n\3
May I know if there are three lines to be inserted at once, how shall I keep the value of the three lines?
I mean it is like
This is my original first line.
This is my original second line.
This is my original third line.To be inserted to the second line position:
Insert sentense 1
Insert sentense 2
Insert sentense 3I tried the following code:
\1Insert sentense 1 Insert sentense 2 Insert sentense 3\r\n\3
I found the above code will break the replacing function.
\1Insert sentense 1. \n Insert sentense 2 \n Insert sentense 3\r\n\3
The above code will work when inserting a paragraph.
-
@NZ-Select said in How to insert text into the second line with RegEx in Notepad ++ ?:
if there are three lines to be inserted at once
Replace the current "Replace With field with
\1New Line 2\r\nNew Line 3\r\nNew Line 4\r\n\3
Obviously each of the New Line 2, New Line 3 and New Line 4 would be your inserted lines.
Alternatively you could run the regex 3 times, first with the 3rd line to be inserted, then the 2nd line and finally by the 1st inserted line. They should appear in the correct order.Did you understand what the regex is doing or do you want a bit of an explanation to better understand what it is doing?
Terry
-
It works for me.
\n^(.+)
txt\n$1My motto:
The simpler the pattern, the better.
-
@Pan-Jan said in How to insert text into the second line with RegEx in Notepad ++ ?:
txt\n$1
I tried the code above, it will insert “txt” to line number 2; if only run once.
-
Thank you.
I don’t understand how exactly it is creating this magic.
I am putting the code here, it does give a little hint on what function each magic tiny code is doing.
https://regex101.com/ -
@NZ-Select said in How to insert text into the second line with RegEx in Notepad ++ ?:
I don’t understand how exactly it is creating this magic
The magic as you mention is having the regex cover the whole file in 1 cycle, instead of what normally happens when a regex completes several cycles by the time the complete file has been processed. That means my regex can ONLY replace/insert characters once.
The first part grabs 1 line ONLY as the (?-s) does NOT allow the
.
to include end of line characters (carriage return and line feed). This is saved as group 1. Group 2 captures the rest of the file as the (?s) allows the.
to include end of line characters.
The “replace with” command writes back groups 1 and 2, but with the inserted lines between them. The\r\\n
are the carriage return and line feed characters.Terry
-
NZ Select;
This code works perfectly:Search
(?-s)^((.+)?\R)(?s)(.+)*
Replace
Replace With:\1New Line 2\r\n\3
++++++++++++++++++++++++++++
You can give an example where this will work,
(?-s)^((.+)?\R)(?s)(.+)*
\1New Line 2\r\n\3and this one will not work anymore?
\n^(.+)
txt\n\1 -
Hello, @nz-select, @ekopalypse, @terry-R, @pan-jan and All,
As @terry-r said, we saw in some previous discussions that the
\A
assertion, representing the zero-length location of the very beginning of current file, isn’t properly handled in the N++ regex engineAfter some tests, we improved the good behavior of this assertion by changing the
\A
syntax with the new form\A^
( or some similar ones )However, there were still of lot of problems, when files began with some empty lines and/or when the replacement part ended with line-break characters
Thus :
-
The first solution is to use the @terry-r solution, which matches the entire contents of each scanned file and works perfectly for usual files However, the regex behavior may be problematic when very big files are concerned ! Luckily, to give you an idea, it correctly inserted a block of
3
lines after the first lines of a file containing100,000
lines about ;-)) -
The second solution is to use the new regexes S/R, below, which solve all normal cases and most of these edge-cases and do not mind about file size !
-
So, here is the generic regex S/R which insert a block of
M
lines after the firstN
(N > 0
) lines, empty or not, of current file :-
SEARCH
(?-s)\A^(?:.*\R){
N-1}.*\K(?=(\R))
-
REPLACE
\1New_Line_1\1New_Line_2\1New_Line_3
-
Of course, change the value
N-1
value by the appropriate integer !-
From this generic regex, it’s easy to deduce the search regex for some small values of N ( same replacement regex ) :
-
SEARCH
(?-s)\A^.*\K(?=(\R))
( Case N =1
=> Insertion after thefirst
line ) -
SEARCH
(?-s)\A^.*\R.*\K(?=(\R))
( Case N =2
=> Insertion after thesecond
line )
-
Notes :
-
In the generic regex above, the value N must be strictly positive, in order that N-1 >=
0
-
These different regexes, above, work whatever the type of files (
Windows
,Unix
orMac
). Current line-break is stored as group1
and is rewritten, in the replacement regex, with the syntax\1
-
If you omit the first
\1
syntax, in replacement, you may join the firstNth
initial line, right after the inserted block ofM
lines !
One example : assuming this sample text :
First initial line Second initial line Third initial line Fourth initial line Fifth initial line Sixth initial line Seventh initial line Eighth initial line Ninth initial line Tenth initial line
The regex S/R :
SEARCH
(?-s)\A^(?:.*\R){5}.*\K(?=(\R))
Insertion after6th
line means that{
N- 1}
={5}
REPLACE
\1New_Line_1\1New_Line_2\1New_Line_3
would return :
First initial line Second initial line Third initial line Fourth initial line Fifth initial line Sixth initial line New_Line_1 New_Line_2 New_Line_3 Seventh initial line Eighth initial line Ninth initial line Tenth initial line
-
In case of an insertion before the
first
NON-empty line, use the following regex S/R :-
SEARCH
(?-s)\A^\R*\K(?=.*(\R))
-
REPLACE
New_Line_1\1New_Line_2\1New_Line_3\1
-
Notes :
-
As before, this regex S/R works whatever the type of files (
Windows
,Unix
orMac
). Current line-break is stored as group1
and is rewritten, in the replacement regex, with the syntax\1
-
If you omit the last
\1
syntax, in replacement, you may join thefirst
non-empty line, right after the inserted block ofM
lines !
Now, note that in order to insert a block of lines before the
first
empty line, we need to use the @terry-r flavor, which matches the integrity of current file, at once. So, the following regex will insert a blockM
lines before thefirst
line , empty or not :SEARCH
(?-s)\A^\K(.*(\R)(?s).*)
REPLACE
New_Line_1\2New_Line_2\2New_Line_3\2\1
Notes :
-
As before, this regex S/R works whatever the type of files (
Windows
,Unix
orMac
). Current line-break is stored as group2
and is rewritten, in the replacement regex, with the syntax\2
-
If you omit the last
\2
syntax, in replacement, you may join thefirst
line, empty or not, right after the inserted block ofM
lines ! -
Note that all the contents of current file are stored as group
1
-
As said before, you may get some issues when executed against files of very important size !
Of course, the @terry-r regex flavor can be generalized ! So, the following regex S/R will insert any block of
M
lines after the firstN
( N >=0
) lines, empty or not, of current file :SEARCH
(?-s)\A^(?:.*\R){
N}\K(.*(\R)(?s).*)
REPLACE
New_Line_1\2New_Line_2\2New_Line_3\2\1
Notes :
-
Of course, if N =
0
, this would mean insertion after line0
, so, practically, insertion before thefirst
line, empty or not ! -
As before, this regex S/R works whatever the type of files (
Windows
,Unix
orMac
). Current line-break is stored as group2
and is rewritten, in the replacement regex, with the syntax\2
-
After the first
N
lines, the remaining contents of current file are stored as group1
-
If you omit the last
\2
syntax, in replacement, you may join the firstNth
line, empty or not, right after the inserted block ofM
lines ! -
As said before, you may get some issues when executed against files of very important size !
Assuming the same example as before, the @terry-r " flavor " regex S/R :
SEARCH
(?-s)\A^(?:.*\R){6}\K(.*(\R)(?s).*)
REPLACE
New_Line_1\2New_Line_2\2New_Line_3\2\1
would give the same output text :
First initial line Second initial line Third initial line Fourth initial line Fifth initial line Sixth initial line New_Line_1 New_Line_2 New_Line_3 Seventh initial line Eighth initial line Ninth initial line Tenth initial line
Best regards,
guy038
-
-