Regex: Insert new line at the beginning of file



  • maybe someone will need this Regex:

    Select only the first line of the text-file ^\A(.*)$

    Insert any word in the beginning of file:
    Search ^\A(.*)$
    Replace with Word $1

    Select the last line of the text-file ^(.*)$\z

    insert word at the end of file
    Search ^(.*)$\z
    Replace with $1 Word

    Add a new empty line at the beginning of file:
    ^\A(.*)$
    Replace cu \r \1

    Add a new empty line after the first line:
    Search ^\A(.*)$
    Replace cu \1 \r (after \r leave an empty space)



  • And to Insert a new line at the end of file
    Search ^(.*)$\z
    Replace by: \1 \r



  • I think I fail to see why these are useful regexes in a Notepad++ context. For example, if I want to select the first line in a file, I just do it. If I want to insert a word at beginning of file, I just do it. And so on. Regexes definitely have their uses, but it seems maybe that you are just playing around and learning about this topic.



  • I wonder myself why, until I had to modify 2000 html files, namely to add a particular php script at the beginning at each html file.

    It helps me a lot !



  • I see. Perhaps in these situations one should look into a text-processing language, e.g. Python, Perl, AWK, sed, etc. If you do so, you will see the benefits of these languages. This has been pointed out many times before on this forum. It of course can be done in Notepad++, but it is good to have many tools in the toolbox so that every problem you have to solve doesn’t start looking like a nail.



  • Notepad with Regex is much easy to use, for me, which I am not a programmer. And, I believe, there are many people like me… Not everyone is a programmer.


Log in to reply