简体   繁体   English

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

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

I want to replace the "<p>" tag with "<p class="myclass">" class in the following html with Search & Replace (using regular expression) function in Notepad++ 我想用记事本++中的搜索和替换(使用正则表达式)功能将以下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>

I was able to match the above with the following reg-ex: 我能够将上述内容与以下正则表达式匹配:

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

I tried the following replace reg-ex but it's not working: 我尝试了以下替换reg-ex,但无法正常工作:

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

It comes out as: <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> <p class="cool"><?php echo $element_arr["[A-Za-z][1-99]_discount"]; ?></p>

Can someone help me with this. 有人可以帮我弄这个吗。 This will be a huge time-saver as I have lots of editing of such nature to do. 这将节省大量时间,因为我需要进行大量此类编辑。

Thanks a lot 非常感谢

dkj DKJ

You don't even need a regex for this. 您甚至不需要正则表达式。 Just search for 只是搜索

<p> and replace it with <p class="myclass"> <p>并将其替换为<p class="myclass">

In case you do have other p tags with various attributes, you can use the following approach: 如果确实还有其他具有各种属性的p标签,则可以使用以下方法:

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

This is working for strings like 这适用于像

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

This is fine for strings like 这对像这样的字符串很好

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

This complexity can be easily removed if you decided to use any HTML parser with a regular programming language. 如果您决定将任何HTML解析器与常规编程语言一起使用,则可以轻松消除这种复杂性。

How about: 怎么样:

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

Where (?=...) is a positive lookahead that makes sure you have <?php echo ... after the tag <p> 其中(?=...)是一个正向前瞻,可确保您在标签<p>之后具有<?php echo ...

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

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