繁体   English   中英

使用正则表达式在Notepad ++中搜索和替换表达式的第一个字符

[英]Searching & Replacing first characters of an expression in Notepad++ using regular expression

我想用记事本++中的搜索和替换(使用正则表达式)功能将以下HTML中的“ <p>”标记替换为“ <p class =“ myclass”>”类

<p><?php echo $element_arr["a1_discount"]; ?></p>
<p><?php echo $element_arr["b2_discount"]; ?></p>
<p><?php echo $element_arr["c5_discount"]; ?></p>

我能够将上述内容与以下正则表达式匹配:

RegEx: (<p><\?php echo \$element_arr\["[A-Za-z][1-99]_discount"\]; \?>)

我尝试了以下替换reg-ex,但无法正常工作:

(<p class="myclass"><\?php echo \$element_arr\["[A-Za-z][1-99]_discount"\]; \?>)

结果显示为: <p class="cool"><?php echo $element_arr["[A-Za-z][1-99]_discount"]; ?></p> <p class="cool"><?php echo $element_arr["[A-Za-z][1-99]_discount"]; ?></p>

有人可以帮我弄这个吗。 这将节省大量时间,因为我需要进行大量此类编辑。

非常感谢

DKJ

您甚至不需要正则表达式。 只是搜索

<p>并将其替换为<p class="myclass">

如果确实还有其他具有各种属性的p标签,则可以使用以下方法:

  1. 使用(<p[^<>]*\\bclass=")[^"]*("[^<>]*>) regex运行S&R,用$1myclass$2替换( Regular Expression选项为ON)。

这适用于像

<p class="someclass">text</p>
<p id="new" class="someclass">text</p>
<p id="new" class="someclass" end="true">text</p></code>
  1. 使用<p\\b(?![^<>]*\\bclass=)([^<>]*)>运行S&R, <p\\b(?![^<>]*\\bclass=)([^<>]*)>没有属性或没有class属性的段落标记替换为<p$1 class="myclass">

这对像这样的字符串很好

<p>text</p>
<p id="new">text</p>

如果您决定将任何HTML解析器与常规编程语言一起使用,则可以轻松消除这种复杂性。

怎么样:

查找内容: <p>(?=<\\?php echo \\$element_arr\\["[A-Za-z][1-9][0-9]*_discount"\\]; \\?>)
替换为: <p class="myclass">

其中(?=...)是一个正向前瞻,可确保您在标签<p>之后具有<?php echo ...

暂无
暂无

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

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