首页 > 解决方案 > 从两个字符串替换特定字符串

问题描述

我想从两个字符串中替换一个特定的字符串。我想删除<br />删除and然后用<blockquote>and替换</blockquote><blockquote><pre></blockquote></pre>

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

最终结果应如下所示 -

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

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

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

标签: regexnotepad++

解决方案


  • Ctrl+H
  • 找什么:\A.*?(?:<blockquote>|\G(?!<))(?:(?!</blockquote>).)*?\K<br />
  • 用。。。来代替:LEAVE EMPTY
  • 取消选中匹配案例
  • 检查环绕
  • 检查正则表达式
  • 查看. matches newline
  • Replace all

解释:

\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>

屏幕截图(之前):

在此处输入图像描述

屏幕截图(之后):

在此处输入图像描述


推荐阅读