簡體   English   中英

如何使用記事本++僅在特定div內的文件中用h3替換h2標簽

[英]How to replace h2 tags by h3 in files only inside a specific div using notepad++

我需要在特定 div 中用 h3 批量替換 h2 標簽。 例子 :

        <div class="txt_contenu">
            <h2 style="text-align: justify;">blabla title</h2>
            <p style="text-align: justify;">&nbsp;</p>
            <p>Lorem ipsum <strong>dolor</strong> sit amet.</p>
            <p style="text-align: justify;">&nbsp;</p>
            <h2 style="text-align: justify;">blabla title 2</h2>
            <p style="text-align: justify;">&nbsp;</p>
            <p>other text example</p>
        </div>

應該變成:

        <div class="txt_contenu">
            <h3 style="text-align: justify;">blabla title</h3>
            <p style="text-align: justify;">&nbsp;</p>
            <p>Lorem ipsum <strong>dolor</strong> sit amet.</p>
            <p style="text-align: justify;">&nbsp;</p>
            <h3 style="text-align: justify;">blabla title 2</h3>
            <p style="text-align: justify;">&nbsp;</p>
            <p>other text example</p>
        </div>

我不太擅長正則表達式,這不是我的代碼,我正在使用記事本++來完成這項工作,但找不到可行的解決方案......

因此,如果有人有一個易於實施的解決方案(我需要在很多 html 文件上執行此操作),我將非常感激。 謝謝。

我試過這個正則表達式:

(<div class=\"txt_contenu\">.*?)(h2)(.*?)(</div>) 

並試圖用 $1h3$3 替換記事本 ++ 中它的工作原理,但它僅針對 div 中的第一個 h2 ...

記事本++的屏幕截圖查找替換

嘗試使用以下正則表達式/過程:

  1. 將光標定位在文件的開頭(手動或使用Control + Home或使用Control + G = 0)
  2. 啟動替換對話框( Control + H
  3. 檢查搜索模式 = 正則表達式
  4. 取消選中Wrap around (重要的是,我們會讓記事本++崩潰😊)
  5. 求什么 => ((?:^|</div>)[\\s\\S]*?<div[ ]class="txt_contenu">)|\\G((?:(?!</div>)[\\s\\S])*?)<h2([\\s\\S]*?)</h2>
  6. 替換為 => (?1\\1)(?3\\2<h3\\3</h3>)
  7. 全部替換

注意:只要<div class="txt_contenu">沒有更多 div,此解決方案就應該有效

如果您可以在<div class="txt_contenu"> (嵌套)中找到其他 div,那么可能可以編寫帶有宏的解決方案。 如果是這種情況,請告訴我。


測試輸入

<h2>foobar</h2>
<div class="whatever">
    <h2>foobar</h2>
</div>
<div class="txt_contenu">
    <h2 style="text-align: justify;">blabla title</h2>
    <p style="text-align: justify;">&nbsp;</p>
    <p>Lorem ipsum <strong>dolor</strong> sit amet.</p>
    <p style="text-align: justify;">&nbsp;</p>
    <h2 style="text-align: justify;">blabla title 2</h2>
    <p style="text-align: justify;">&nbsp;</p>
    <p>other text example</p>
</div>
<h2>foobar</h2>
<div class="whatever">
    <h2>foobar</h2>
</div>
<div class="txt_contenu">
    <h2 style="text-align: justify;">blabla title</h2>
    <p style="text-align: justify;">&nbsp;</p>
    <p>Lorem ipsum <strong>dolor</strong> sit amet.</p>
    <p style="text-align: justify;">&nbsp;</p>
    <h2 style="text-align: justify;">blabla title 2</h2>
    <p style="text-align: justify;">&nbsp;</p>
    <p>other text example</p>
</div>
<h2>foobar</h2>
<div class="whatever">
    <h2>foobar</h2>
</div>

測試輸出

<h2>foobar</h2>
<div class="whatever">
    <h2>foobar</h2>
</div>
<div class="txt_contenu">
    <h3 style="text-align: justify;">blabla title</h3>
    <p style="text-align: justify;">&nbsp;</p>
    <p>Lorem ipsum <strong>dolor</strong> sit amet.</p>
    <p style="text-align: justify;">&nbsp;</p>
    <h3 style="text-align: justify;">blabla title 2</h3>
    <p style="text-align: justify;">&nbsp;</p>
    <p>other text example</p>
</div>
<h2>foobar</h2>
<div class="whatever">
    <h2>foobar</h2>
</div>
<div class="txt_contenu">
    <h3 style="text-align: justify;">blabla title</h3>
    <p style="text-align: justify;">&nbsp;</p>
    <p>Lorem ipsum <strong>dolor</strong> sit amet.</p>
    <p style="text-align: justify;">&nbsp;</p>
    <h3 style="text-align: justify;">blabla title 2</h3>
    <p style="text-align: justify;">&nbsp;</p>
    <p>other text example</p>
</div>
<h2>foobar</h2>
<div class="whatever">
    <h2>foobar</h2>
</div>

使用正則表達式 preg_replace<h2> 至</h2><h2 with id< div><div id="text_translate"><p> 我有一個for循環,我想使用 preg_replace 將 ID 添加到我的&lt;h2&gt;和&lt;h3&gt; ,我的問題是我無法獲得具有 class 的標簽(就像這個&lt;h2 class="ss"&gt;test&lt;/h2&gt; )。 PS:標簽之間的文本很重要,我將它與$r[$i]變量一起傳遞。</p><pre> for ($i = 0; $i &lt; count($r); $i++) { $replace_quary = "&lt;h2 id='t". $i. "'&gt;". $r[$i]. "&lt;/h2&gt;"; $content = preg_replace('#&lt;h2.*?&gt;('. $r[$i].')&lt;/h2&gt;#i', $replace_quary, $content); echo $content; }</pre></div></h2>

[英]Using Regex preg_replace <h2> to <h2 with ID

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM