加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 替换换行符,但标记内部带有括号(<>)的内部

发布时间:2020-12-14 05:48:06 所属栏目:百科 来源:网络整理
导读:我使用 question中提供的答案替换了预标签之外的所有换行符. n(?![^]*/pre) 它一直工作正常,直到预标签中的内容具有或者括号. 例如,输入: pTest contennt for regexwith line breaks/pprecode block with multi line contentworking fine/prepre class="b
我使用 question中提供的答案替换了预标签之外的所有换行符.

n(?![^<]*</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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读