正则表达式:复制上面一行中的单词/字符串

正则表达式:复制上面一行中的单词/字符串

我想复制上面一行中的单词/字符串。例如,我想TWO-2从第二行复制到第一行,替换单词ONE-1

BEBE ONE-1 NERO

text text

OANA TWO-2 BOGDAN

必须成为

BEBE TWO-2 NERO

text text

OANA TWO-2 BOGDAN

在其他情况下,我有类似这样的事情:

  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

必须成为:

  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

我的正则表达式非常接近一个简单的解决方案,但我认为替换不是很好:

查找(.匹配换行符):(BEBE(.*?)NERO.*?)(OANA(.*?)BOGDAN)

替换为:\4\1\3\2

答案1

  • Ctrl+H
  • 找什么:(?<=BEBE ).+?(?= NERO.+?OANA (\S+) BOGDAN)
  • 用。。。来代替:$1
  • 查看 相符
  • 查看 环绕
  • 查看 正则表达式
  • 查看 . matches newline
  • Replace all

解释:

(?<=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
)

替代品:

$1                  # content of group 1

截图(之前):

在此处输入图片描述

截图(之后):

在此处输入图片描述

相关内容