2015-11-04 70 views
1

是的,我知道「不要用正則表達式解析HTML」。我在記事本++中這樣做,這是一次性的事情,所以請耐心等待一會兒。正則表達式僅匹配html元素的第一個匹配項

我試圖通過使用一些更先進的技術來簡化一些HTML代碼。值得注意的是,在我的文檔中,我有「插入」或「標註」或其他任何您稱之爲「註釋」,「警告」和「技術」的短語,以引起讀者注意重要信息:

<div class="note"> 
    <p><strong>Notes</strong>: This icon shows you something that complements 
    the information around it. Understanding notes is not critical but 
    may be helpful when using the product.</p> 
</div> 
<div class="warning"> 
    <p><strong>Warnings</strong>: This icon shows information that may 
    be critical when using the product. 
    It is important to pay attention to these warnings.</p> 
</div> 
<div class="technical"> 
    <p><strong>Technical</strong>: This icon shows technical information 
    that may require some technical knowledge to understand. </p> 
</div> 

我想這個HTML簡化爲以下:

<div class="box note"><strong>Notes</strong>: This icon shows you something that complements 
    the information around it. Understanding notes is not critical but 
    may be helpful when using the product.</div> 
<div class="box warning"><strong>Warnings</strong>: This icon shows information that may 
    be critical when using the product. 
    It is important to pay attention to these warnings.</div> 
<div class="box technical"><strong>Technical</strong>: This icon shows technical information 
    that may require some technical knowledge to understand.</div> 

幾乎要做一個很好的全局搜索&替換從記事本+ +我的項目所需要的正則表達式,但它不是拿起「唯一」第一個div,它正在拾取所有的 - 如果我的光標在我的fi的開頭le,當我點擊查找時,「選擇」是從第一個<div class="something">直到最後的</div>,本質上。

這裏是我的表達:<div class="(.*[^"])">[^<]*<p>(.*?)<\/p>[^<]*<\/div>(記事本+ +「自動」添加/ /周圍,還挺)。

我在做什麼錯,在這裏?

+1

保持您的操作員不願意的好工作。我試着改變這個部分:'class =「(。* [^」])「'to'class =」([^「] *)」'開始。 – Welbog

回答

1

你有一個貪心點量詞同時匹配class屬性 - 這是誰是造成你的問題邪惡的傢伙。

使其非貪婪:<div class="(.*?[^"])">或將其更改爲字符類別:<div class="([^"]*)">

比較:greedy classnon-greedy class

+0

我知道這是一個愚蠢的語法錯誤。謝謝你,亞歷克斯! –