Community
    • 登入

    Regex: Copy a word/string below in a line above

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

      I want to copy a word/string below in a line above. For example I want to copy TWO-2 from the second line into the first line, replacing the word ONE-1

      BEBE ONE-1 NERO
      
      text text
      
      OANA TWO-2 BOGDAN
      

      MUST BECOME

      BEBE TWO-2 NERO
      
      text text
      
      OANA TWO-2 BOGDAN
      

      I believe my regex is close to a simple solution, but I believe the replacement is not very good:

      FIND (.matches newline): (BEBE(.*?)NERO.*?)(OANA(.*?)BOGDAN)

      REPLACE BY: \4\1\3\2

      1 條回覆 最後回覆 回覆 引用 1
      • guy038G
        guy038
        最後由 guy038 編輯

        Hello @robin-Cruise and All,

        Again, the description of your goal is not accurate enough ! Do you mean :

        • I want to substitute the 2nd pseudo word of any line, beginning with the word BEBE, with this exact case, with the 2nd pesudo word of the first line, downwards, beginning with the word OANA, with this exact case. If so, use the following regex S/R :

        SEARCH (?s-i)(^BEBE\h+)[\w-]+(?=.*?^OANA\h+([\w-]+))

        REPLACE \1\2

        • I want to substitute the 2nd pseudo word of any line, beginning with the word BEBE and followed with the word NERO , with these exact case, with the 2nd pseudo word of the first line, downwards, beginning with the word OANA and followed with the word BOGDAN, with these exact case. If so, use the following regex S/R :

        SEARCH (?s-i)(^BEBE\h+)[\w-]+(?=\h+NERO.*?^OANA\h+([\w-]+)\h+BOGDAN)

        REPLACE \1\2

        • I want to substitute the 2nd pseudo word of any line, beginning with the word BEBE and ending with the word NERO , with these exact case, with the 2nd pseudo word of the first line, downwards, beginning with the word OANA and ending with the word BOGDAN, with these exact case. If so, use the following regex S/R :

        SEARCH (?s-i)(^BEBE\h+)[\w-]+(?=(?-s:.*?)\h+NERO$.*?^OANA\h+([\w-]+)(?-s:.*?)\h+BOGDAN$)

        REPLACE \1\2


        Note that I speak of pseudo words for strings, like ONE-1, which are matched by the [\w-]+ regex !

        You can test these three regex S/R against this sample text :

        ==============================
        BEBE ONE-1 NERO AAZ-ZZZ
        
        text text
        text
        
        OANA TWO-2 BOGDAN AAA-ZZZ
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        BEBE ONE-1 AAA-AAA NERO
        
        text text
        text
        
        OANA THREE-3 ZZZ-ZZZ BOGDAN
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        BEBE ONE-1 ROBIN
        
        text text
        text
        
        OANA TEST-0 GUY
        ==============================
        BEBE ONE-1 NERO AAZ-ZZZ
        
        text text
        text
        
        OANA TWO-2 BOGDAN AAA-ZZZ
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        BEBE ONE-1 AAA-AAA NERO
        
        text text
        text
        
        OANA THREE-3 ZZZ-ZZZ BOGDAN
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        BEBE ONE-1 ROBIN
        
        text text
        text
        
        OANA TEST-0 GUY
        ==============================
        

        If any problem about regexe’s syntax, just tell me !

        Best Regards,

        guy038

        1 條回覆 最後回覆 回覆 引用 2
        • Robin CruiseR
          Robin Cruise
          最後由 編輯

          Super formula, sir @guy038 Thank you.

          Now, if you can. For a complete answer. How about vice-versa?

          What If I want to change the word on top with the word on the second case? So as to copy ONE-1 from first line to the second, as to become OANA ONE-1 BOGDAN ?

          Can you add an answer with a regex formula for the second case?

          1 條回覆 最後回覆 回覆 引用 0
          • guy038G
            guy038
            最後由 guy038 編輯

            Hi, @robin-Cruise and All,

            As the regex engine always processes from top to bottom, the groups to create are different but there is no extra difficulty to build up the regexes ! So, with the same scheme, used in my previous script :

            • If you want to substitute the 2nd pseudo word of any line, beginning with the word OANA, with this exact case, with the 2nd pseudo word of the first line, upwards, beginning with the word BEBE, with this exact case, use the following regex S/R :

            SEARCH (?s-i)(^BEBE\h+([\w-]+).*?^OANA\h+)[\w-]+

            REPLACE \1\2

            • If you want to substitute the 2nd pseudo word of any line, beginning with the word OANA and followed with the word NERO, with these exact case, with the 2nd pseudo word of the first line, upwards, beginning with the word BEBE and followed with the word BOGDAN, with these exact case, use the following regex S/R :

            SEARCH (?s-i)(^BEBE\h+([\w-]+)\h+NERO.*?^OANA\h+)[\w-]+(?=\h+BOGDAN)

            REPLACE \1\2

            • If you want to substitute the 2nd pseudo word of any line, beginning with the word OANA and ending with the word NERO, with these exact case, with the 2nd pseudo word of the first line, upwards, beginning with the word BEBE and ending with the word BOGDAN, with these exact case, use the following regex S/R :

            SEARCH (?s-i)(^BEBE\h+([\w-]+)(?-s:.*?)\h+NERO$.*?^OANA\h+)[\w-]+(?=(?-s:.*?)\h+BOGDAN$)

            REPLACE \1\2


            You can test these three regex S/R against this new sample text, below :

            ==============================
            BEBE ONE-1 NERO AAZ-ZZZ
            
            text text
            text
            
            OANA TWO-2 BOGDAN AAA-ZZZ
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            BEBE THREE-3 AAA-AAA NERO
            
            text text
            text
            
            OANA TWO-2 ZZZ-ZZZ BOGDAN
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            BEBE TEST-0 ROBIN
            
            text text
            text
            
            OANA TWO-2 GUY
            ==============================
            BEBE ONE-1 NERO AAZ-ZZZ
            
            text text
            text
            
            OANA TWO-2 BOGDAN AAA-ZZZ
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            BEBE THREE-3 AAA-AAA NERO
            
            text text
            text
            
            OANA TWO-2 ZZZ-ZZZ BOGDAN
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            BEBE TEST-0 ROBIN
            
            text text
            text
            
            OANA TWO-2 GUY
            ==============================
            

            Best Regards,

            guy038

            1 條回覆 最後回覆 回覆 引用 1
            • Robin CruiseR
              Robin Cruise
              最後由 編輯

              thank you very much sir @guy038

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