0

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

In other case, I have something like this:

  html code ... several lines
  text <p> BEBE ONE-1 NERO <div> text...
    
  other  html code ... several lines
    
  text <table> OANA TWO-2 BOGDAN <tr> text
  again html code ... several lines

MUST BECOME:

  html code ... several lines
  text <p> BEBE TWO-2 NERO <div> text...
    
  other  html code ... several lines
    
  text <table> OANA TWO-2 BOGDAN <tr> text
  again html code ... several lines

My regex is very 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 Answer 1

2
  • Ctrl+H
  • Find what: (?<=BEBE ).+?(?= NERO.+?OANA (\S+) BOGDAN)
  • Replace with: $1
  • CHECK Match case
  • CHECK Wrap around
  • CHECK Regular expression
  • CHECK . matches newline
  • Replace all

Explanation:

(?<=BEBE )          # positive lookbehind, make sure we have "BEBE " before (note the space)
.+?                 # 1 or more any character, not greedy
(?=                 # positive lookahead, make sure we have after:
 NERO               # space, NERO
.+?                 # 1 or more any character, not greedy
OANA                # OANA, space
(\S+)               # group 1, 1 or more non spaces, you can use (.+?)
 BOGDAN             # space, BOGDAN
)

Replacement:

$1                  # content of group 1

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

4
  • thanks. Your regex is very good, thanks
    – Just Me
    Commented May 15, 2021 at 11:16
  • How about vice-versa? 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 update your answer with a regex formula for the second case?
    – Just Me
    Commented May 15, 2021 at 11:24
  • 1
    @JustMe: Have a try with: Find: BEBE (.+?) NERO.+?OANA \K\S+(?= BOGDAN) Replace: $1. If it doesn't give the result you want, please, ask a new question with formated input string and expected result.
    – Toto
    Commented May 15, 2021 at 11:59
  • works @Toto, thanks a lot. Please update your answer with the second case. If I post a new question, it can be deleted, because is too similar
    – Just Me
    Commented May 15, 2021 at 12:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .