gpt4 book ai didi

regex - 用
 标签内的方括号 (<>) 替换除 
 标签内的换行符

转载 作者:行者123 更新时间:2023-12-02 11:23:00 28 4
gpt4 key购买 nike

我使用 question 中提供的答案替换了 pre 标签外的所有换行符.

\n(?![^<]*<\/pre>)

它工作正常,直到 pre 标签中的内容有 < 或 > 括号。

例如,输入:
<p>Test contennt for regex
with line breaks</p>
<pre>code block
with multi line content
working fine</pre>
<pre class="brush:C#">
test line break before
open paranthesis < is not working fine
line breaks after paranthesis
is accepted
</pre>

输出是
<p>Test contennt for regexwith line breaks</p><pre>code block 
with multi line content
working fine</pre><pre class="brush:C#">test line break before open paranthesis < is not working fine
line breaks after paranthesis
is accepted
</pre>

这是不正确的 - 并非所有换行符都被删除。

this regex101 .

最佳答案

尝试这个:

/\n(?=((?!<\/pre).)*?(<pre|$))/sg

这个想法是有一个大的前瞻性。这
((?!<\/pre).)*?

重复匹配任何字符(包括带有 . 的换行符),并且其后跟
(<pre|$)

要求上述字符不是 <</pre .然后,匹配 <pre (表示原始换行符不在 <pre 内,或与文件末尾匹配。

https://regex101.com/r/cjZQO9/2

随着输入
<p>Test contennt for regex
with line breaks</p>
<pre>code block
with multi line content
working fine</pre>
text
more text
<pre class="brush:C#">
test line break before
open paranthesis < is not working fine
line breaks after paranthesis
is accepted
</pre>
text

输出是
<p>Test contennt for regexwith line breaks</p><pre>code block 
with multi line content
working fine</pre>textmore text<pre class="brush:C#">
test line break before
open paranthesis < is not working fine
line breaks after paranthesis
is accepted
</pre>text

关于regex - 用 <pre> 标签内的方括号 (<>) 替换除 <pre> 标签内的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51510735/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com