Community
    • 登入

    Use regex to replace backspace by underscore

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    7 貼文 3 Posters 8.8k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Mauricio SalazarM
      Mauricio Salazar
      最後由 編輯

      Hi everybody I have a csv file in which I would like to make the following replacement:

      Input:
      Word, Word

      Output:
      Word_Word

      I want to replace words with the following characteristics:

      1. Longer than four characters
      2. The first word ends with a comma
      3. There is a backspace between the words

      I tried this regular expression with the TextFX, but I did not work:

      Find:
      ^[A-Za-z],{3,23}$\b^[A-Za-z]{3,23}$

      Replace:
      ^[A-Za-z],{3,23}$[_]^[A-Za-z]{3,23}$

      Thank you

      gerdb42G 1 條回覆 最後回覆 回覆 引用 0
      • gerdb42G
        gerdb42 @Mauricio Salazar
        最後由 編輯

        With NPPs Search&Replace you would use ^(\w{5,}),\x08(.*)$ to search and $1_$2 for replacement. This will search for any word at the beginning of a line which is 5 or more characters long and is followed by a colon followed by a backspace. Both the word and the remainder of the line are grouped. In the replacement, the word followed by an underscore followed by the rest of the original line is used.

        You may want to read about the Regular Expression syntax here for searching and here for replacing.

        1 條回覆 最後回覆 回覆 引用 0
        • Mauricio SalazarM
          Mauricio Salazar
          最後由 Mauricio Salazar 編輯

          Thank you for your help, but I wonder if I am applying the regex correctly.

          http://postimg.org/image/tb98dqnf7/

          As you see in the image I did not change. With “NPPs Search&Replace” you meant a plug in.

          Thank you

          1 條回覆 最後回覆 回覆 引用 0
          • dailD
            dail
            最後由 編輯

            From your image, it doesn’t look like there is a backspace after the comma.

            img

            1 條回覆 最後回覆 回覆 引用 0
            • Mauricio SalazarM
              Mauricio Salazar
              最後由 編輯

              You are right, is was a noob error backspace ≠ whitespace,. So I followed this post, link, that suggest using: (\s).

              Alt text

              Could you please help me changing the regex from backspace to whitespace?

              Best Regards

              1 條回覆 最後回覆 回覆 引用 0
              • dailD
                dail
                最後由 dail 編輯

                ^(\w{5,}),\s(.*)$

                Instead of \x08 which represents 1 backspace character you just use \s which is a single whitespace chracter.

                1 條回覆 最後回覆 回覆 引用 0
                • Mauricio SalazarM
                  Mauricio Salazar
                  最後由 編輯

                  Thank you, it worked well

                  Best Regards

                  1 條回覆 最後回覆 回覆 引用 0
                  • 第一個貼文
                    最後的貼文
                  The Community of users of the Notepad++ text editor.
                  Powered by NodeBB | Contributors