How to insert text into the second line with RegEx in Notepad ++ ?



  • I have a list of txt files under a directory.
    I wish to insert a fixed block of text into each of the txt files, but only add into the second line; and the original second line content will automatically become the third line.

    Example:
    Original:

    This is my original first line.
    This is my original second line.
    This is my original third line.

    After RegEx operation, the content becomes

    This is my original first line.
    This is my inserted content, it becomes the second line.
    This is my original second line.
    This is my original third line.

    Thank you in advance!



  • Find: (\n^(.+)$)
    Replace: inserted line$1



  • @Pan-Jan
    No, does not work.
    It will insert the new text multiple times.



  • Schowek01.jpg
    it is supposed to look like this

    1. you click find
    2. you click replace

    and that’s all



  • @Pan-Jan

    but the OP looks for a replacement in multiple files.
    Not sure OP likes to open each file first. Could be hundreds of it.



  • Hello, @nz-select, @pan-jan, @ekopalypse and All,

    @nz-select, one question :

    • Do some of your files begin with true empty lines file OR each file begins with a non-empty line ?

    See you later,

    Best Regards

    guy038



  • Schowek00.jpg
    Schowek01.jpg
    Schowek02.jpg



  • @NZ-Select said in How to insert text into the second line with RegEx in Notepad ++ ?:

    but only add into the second line

    It seemed to be a relatively easy problem to solve, however it wasn’t. I’ll reference a conversation I was involved in about a year ago on the very issue of how to reference the start of a file:
    https://community.notepad-plus-plus.org/topic/15998/regex-anchor-for-beginning-of-file?_=1595369289169

    The use of \A and indeed my suggested replacement does not work in this instance. Back then we didn’t consider the “Find in Files” function, only opened files within Notepad++. Maybe @guy038 will be able to come up with a guaranteed solution and possibly even a reason behind the failure of the options presented in that conversation. Until then I have a solution which “appears” to work, even on a file of around 6MB, and 90K plus lines.

    So using the “Find in Files” function, you’d insert the appropriate filters, directory and tick if sub-folders were to be included. Search mode is obviously “regular expression”. The regex is:
    Find What:(?-s)^((.+)?\R)(?s)(.+)*
    Replace With:\1New Line 2\r\n\3
    You need to replace New Line 2 (in the Replace With field) with your line to be inserted. If any of the characters to be inserted are “meta-characters” (such as ^$*?(){}[] and others) you will need to precede them with the “escape” key \.
    Click the “Find in Files” button and OK the query that appears (are you sure?).

    As always, it pays to have a backup copy of the files before running the first time and to verify some randomly.

    Good luck
    Terry



  • @Pan-Jan

    I think you missed the point. The file won’t already be open in NPP, instead you’d use the “Find in Files” tab that you can see in your images. This allows you to work on MANY files at once using the same regex that you would on a single file opened in NPP. In that tab the button is “Replace in Files” which I believe is equivalent to a “Replace All” if used on an opened file.

    Terry



  • I didn’t understand there were more open files.
    I’ll work on that later.



  • @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\3

    My notes:
    There is a 1 before the intended insert text.

    P.s I do not need to open my files first.



  • @Terry-R

    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 3

    I 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$1

    My motto:
    The simpler the pattern, the better.
    Schowek01.jpg



  • @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.



  • @Terry-R

    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\3

    and 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 engine

    After 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 containing 100,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 first N ( 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 the first line )

      • SEARCH (?-s)\A^.*\R.*\K(?=(\R))   ( Case N = 2 => Insertion after the second 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 or Mac ). Current line-break is stored as group 1 and is rewritten, in the replacement regex, with the syntax \1

    • If you omit the first \1 syntax, in replacement, you may join the first Nth initial line, right after the inserted block of M 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 after 6th 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 or Mac ). Current line-break is stored as group 1 and is rewritten, in the replacement regex, with the syntax \1

    • If you omit the last \1 syntax, in replacement, you may join the first non-empty line, right after the inserted block of M 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 block M lines before the first 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 or Mac ). Current line-break is stored as group 2 and is rewritten, in the replacement regex, with the syntax \2

    • If you omit the last \2 syntax, in replacement, you may join the first line, empty or not, right after the inserted block of M 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 first N ( 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 line 0, so, practically, insertion before the first line, empty or not !

    • As before, this regex S/R works whatever the type of files ( Windows, Unix or Mac ). Current line-break is stored as group 2 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 group 1

    • If you omit the last \2 syntax, in replacement, you may join the first Nth line, empty or not, right after the inserted block of M 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


Log in to reply