Community
    • Login

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

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 2 Posters 745 Views 1 Watching
    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.
    • Robin CruiseR Offline
      Robin Cruise
      last edited by

      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 Reply Last reply Reply Quote 1
      • guy038G Offline
        guy038
        last edited by 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 Reply Last reply Reply Quote 2
        • Robin CruiseR Offline
          Robin Cruise
          last edited by

          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 Reply Last reply Reply Quote 0
          • guy038G Offline
            guy038
            last edited by 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 Reply Last reply Reply Quote 1
            • Robin CruiseR Offline
              Robin Cruise
              last edited by

              thank you very much sir @guy038

              1 Reply Last reply Reply Quote 1

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • First post
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors