![](/img/trans.png)
[英]Searching and replacing text in html file with notepad++ using regular expression
[英]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
標簽,則可以使用以下方法:
(<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>
<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.