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



  • 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



  • 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



  • 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?



  • 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



  • thank you very much sir @guy038


Log in to reply