简体   繁体   English

正则表达式删除标签Notepad ++中的特定文本

[英]regex remove specific text inside tag Notepad++

Hello i'm a bit new to coding and trying to understand how regular expressions works, 您好,我对编码和尝试了解正则表达式的工作方式有些陌生,

so i'm working on an XML file with products and want to delete specific text inside a tag. 所以我正在使用产品处理XML文件,并想删除标签内的特定文本。 From the example below: 从下面的示例:

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

i want to remove this part: 我要删除此部分:

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

cause its interfering with the text format. 导致其干扰文本格式。 This may happen multiple times within the tag, so i want to remove it every time. 标记内可能会发生多次,因此我想每次都将其删除。 Can i do this with a regex in Notepad++ ? 我可以在Notepad ++中使用正则表达式吗?

I was able to do your replace with this regex: 我能够用此正则表达式来代替您:

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

Replacing it by: 替换为:

$1 SUCCESS $2

I used as input: 我用作输入:

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

And it became: 它变成了:

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

Image: 图片:

在此处输入图片说明

Explaining the regex: 解释正则表达式:

(                                 # 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

And the replace: 和替换:

$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM