2015-11-04 71 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

我知道这是一个愚蠢的语法错误。谢谢你,亚历克斯! –