我想从两个字符串中替换一个特定的字符串。我想删除<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>
答案 0 :(得分:3)
\A.*?(?:<blockquote>|\G(?!<))(?:(?!</blockquote>).)*?\K<br />
LEAVE EMPTY
. 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>
屏幕截图(之前):
屏幕截图(之后):