gpt4 book ai didi

javascript - 正则表达式删除标记Notepad++内的特定文本

转载 作者:行者123 更新时间:2023-12-01 03:22:31 25 4
gpt4 key购买 nike

您好,我对编码有点陌生,并试图了解正则表达式的工作原理,

所以我正在处理一个包含产品的 XML 文件,并且想要删除标签内的特定文本。从下面的例子来看:

<descr>&lt;br/&gt;
&lt;P&gt;SOME RANDOM TEXT&lt;/P&gt;&lt;br/&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;br/&gt;</descr>

我想删除这部分:

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

导致它干扰文本格式。这可能会在标签内发生多次,所以我想每次都将其删除。我可以使用 Notepad++ 中的正则表达式来执行此操作吗?

最佳答案

我能够使用此正则表达式进行替换:

(<descr>[\s\S]*?)&lt;P&gt;&amp;nbsp;&lt;\/P&gt;([\s\S]*?<\/descr>)

替换为:

$1 SUCCESS $2

我用作输入:

<descr>&lt;br/&gt;
&lt;P&gt;SOME RANDOM TEXT&lt;/P&gt;&lt;br/&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;br/&gt;</descr>

<other>&lt;br/&gt;
&lt;P&gt;SOME RANDOM TEXT&lt;/P&gt;&lt;br/&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;br/&gt;</other>

<descr>&lt;br/&gt;
&lt;P&gt;SOME RANDOM TEXT&lt;/P&gt;&lt;br/&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;br/&gt;</descr>

它变成了:

<descr>&lt;br/&gt;
&lt;P&gt;SOME RANDOM TEXT&lt;/P&gt;&lt;br/&gt;
SUCCESS &lt;br/&gt;</descr>

<other>&lt;br/&gt;
&lt;P&gt;SOME RANDOM TEXT&lt;/P&gt;&lt;br/&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;br/&gt;</other>

<descr>&lt;br/&gt;
&lt;P&gt;SOME RANDOM TEXT&lt;/P&gt;&lt;br/&gt;
SUCCESS &lt;br/&gt;</descr>

图片:

enter image description here

解释正则表达式:

(                                 # start of group 1
<descr> # match the open tag
[\s\S] # space or non-space characters = anything
*? # the minimum amount till the next match
) # end of group 1
&lt;P&gt;&amp;nbsp;&lt;\/P&gt; # your pattern, please note I had to escape the slash
( # start of group 2
[\s\S] # space or non-space characters = anything
*? # the minimum amount till the next match
<\/descr> # the closing tag, again look the escaped slash
) # end of group 2

替换:

$1 SUCCESS $2                     # $1 stores the value matched by the group 1
# $2 stores the value matched by the group 2
# The text " SUCCESS " was an example, it could be empty

关于javascript - 正则表达式删除标记Notepad++内的特定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45078053/

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