Community
    • 登入

    Regex: Insert new line at the beginning of file

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    6 貼文 2 Posters 8.5k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Vasile CarausV
      Vasile Caraus
      最後由 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 條回覆 最後回覆 回覆 引用 1
      • Vasile CarausV
        Vasile Caraus
        最後由 編輯

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

        1 條回覆 最後回覆 回覆 引用 1
        • Scott SumnerS
          Scott Sumner
          最後由 編輯

          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 條回覆 最後回覆 回覆 引用 0
          • Vasile CarausV
            Vasile Caraus
            最後由 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 條回覆 最後回覆 回覆 引用 0
            • Scott SumnerS
              Scott Sumner
              最後由 編輯

              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 條回覆 最後回覆 回覆 引用 0
              • Vasile CarausV
                Vasile Caraus
                最後由 編輯

                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 條回覆 最後回覆 回覆 引用 0
                • 第一個貼文
                  最後的貼文
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors