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-2from the second line into the first line, replacing the wordONE-1BEBE ONE-1 NERO text text OANA TWO-2 BOGDANMUST BECOME
BEBE TWO-2 NERO text text OANA TWO-2 BOGDANI 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
2ndpseudo word of any line, beginning with the wordBEBE, with this exact case, with the2ndpesudo word of the first line, downwards, beginning with the wordOANA, 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
2ndpseudo word of any line, beginning with the wordBEBEand followed with the wordNERO, with these exact case, with the2ndpseudo word of the first line, downwards, beginning with the wordOANAand followed with the wordBOGDAN, 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
2ndpseudo word of any line, beginning with the wordBEBEand ending with the wordNERO, with these exact case, with the2ndpseudo word of the first line, downwards, beginning with the wordOANAand ending with the wordBOGDAN, 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
- I want to substitute the
-
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-1from first line to the second, as to becomeOANA 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
2ndpseudo word of any line, beginning with the wordOANA, with this exact case, with the2ndpseudo word of the first line, upwards, beginning with the wordBEBE, 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
2ndpseudo word of any line, beginning with the wordOANAand followed with the wordNERO, with these exact case, with the2ndpseudo word of the first line, upwards, beginning with the wordBEBEand followed with the wordBOGDAN, 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
2ndpseudo word of any line, beginning with the wordOANAand ending with the wordNERO, with these exact case, with the2ndpseudo word of the first line, upwards, beginning with the wordBEBEand ending with the wordBOGDAN, 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
- If you want to substitute the
-
thank you very much sir @guy038