从两个字符串中替换特定的字符串

时间:2019-09-07 12:56:40

标签: regex notepad++

我想从两个字符串中替换一个特定的字符串。我想删除<br /><blockquote>之间的</blockquote>,然后将<blockquote>替换为<pre>,将</blockquote>替换为</pre>

<blockquote>
data.frame()<br />
mutate()<br /></blockquote>

最终结果应如下所示-

<pre>
data.frame()
mutate()
</pre>

我尝试了以下解决方案,但是它选择了块引用之间的所有内容。

(?s)^\t*<blockquote>(.|\r\n)*?</blockquote>

1 个答案:

答案 0 :(得分:3)

  • Ctrl + H
  • 查找内容:\A.*?(?:<blockquote>|\G(?!<))(?:(?!</blockquote>).)*?\K<br />
  • 替换为:LEAVE EMPTY
  • UNcheck区分大小写
  • 检查环绕
  • 检查正则表达式
  • 检查. matches newline
  • 全部替换

说明:

\A                      # beginning of string
.*?                     # 0 or more any character, not greedy
(?:                     # start non capture group
  <blockquote>          # literally open tag
 |                      # OR
  \G                    # restart from last match position
  (?!<)                 # negative lookahead, make sure we haven't "<" after
)                       # end group
(?:                     # start non capture group
  (?!</blockquote>)     # negative lookahead, make sure we haven't after a closing tag
  .                     # any character
)*?                     # end group, may appear 0 or more times, not greedy
\K                      # forget all we have seen until this position
<br />                  # literally

给出:

<tag>
blah<br />
blah<br />
</tag>
<blockquote>
data.frame()<br />
mutate()<br /></blockquote>
<tag>
blah<br />
blah<br />
</tag>
<blockquote>
data.frame()<br />
mutate()<br /></blockquote>
<tag>
blah<br />
blah<br />
</tag>

给定示例的结果

<tag>
blah<br />
blah<br />
</tag>
<blockquote>
data.frame()
mutate()</blockquote>
<tag>
blah<br />
blah<br />
</tag>
<blockquote>
data.frame()
mutate()</blockquote>
<tag>
blah<br />
blah<br />
</tag>

屏幕截图(之前):

enter image description here

屏幕截图(之后):

enter image description here