Community
    • Login

    Regex: Insert new line at the beginning of file

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 2 Posters 8.3k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Vasile CarausV
      Vasile Caraus
      last edited by Vasile Caraus

      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)

      1 Reply Last reply Reply Quote 1
      • Vasile CarausV
        Vasile Caraus
        last edited by

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

        1 Reply Last reply Reply Quote 1
        • Scott SumnerS
          Scott Sumner
          last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • Vasile CarausV
            Vasile Caraus
            last edited by Vasile Caraus

            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 !

            1 Reply Last reply Reply Quote 0
            • Scott SumnerS
              Scott Sumner
              last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • Vasile CarausV
                Vasile Caraus
                last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors