RegEx将div中的内容与特定类匹配 [英] RegEx match content inside div with specific class

查看:65
本文介绍了RegEx将div中的内容与特定类匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何匹配具有特定类的所有div的内容.例如:

 < div class ="column-box-description paddingT05"> content</div> 

解决方案

通常,除非您可以对要匹配的文本做出强有力的假设,否则不应该使用正则表达式进行此操作.使用实际解析HTML的内容,您会做得更好.

但是,如果您可以做出这些更强的假设,则可以使用:

 < div class ="[^"] *?paddingT05 [^] *?">(.*?)< \/div> 

关键部分是勉强的量词 *?,它与可能的最小文本匹配(即,它不会贪婪地吃掉</div> .

How can I match the contents of all divs that have a specific class. For example:

  <div class="column-box-description paddingT05">content</div>

解决方案

Generally, you shouldn't do this with regex unless you can make strong assumptions about the text you're matching; you'll do better with something that actually parses HTML.

But, if you can make these stronger assumptions, you can use:

<div class="[^"]*?paddingT05[^"]*?">(.*?)<\/div>

The key part is the reluctant quantifier *? which matches the minimal text possible (i.e. it doesn't greedily eat up the </div>.

这篇关于RegEx将div中的内容与特定类匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆