使用正则表达式匹配具有特定类的div块

时间:2014-04-05 05:33:04

标签: c# regex

将div块与特定类“report-content”匹配

Regex using: /<div\\sclass="\report-content\">(.*?)<\/div>/

<div class="content"></div>

<div class="report-content">
Are you falling sick without any apparent illness? 
Your job could be the reason behind it. Check the list
of jobs that could jeopardize your health and send you to the doctor. 
</div>


<div class="report-content">
Are you falling sick without any apparent illness? 
Your job could be the reason behind it. Check the list
of jobs that could jeopardize your health and send you to the doctor.
</div>

3 个答案:

答案 0 :(得分:0)

这可行,试试吧:

  /<div class=\"report-content\">(.*?)<\/div>/

答案 1 :(得分:0)

您的代码可以正常进行微调。此外,您应该包含s修饰符以匹配换行符,如下所示:

/<div\ class=\"report-content\">(.*?)<\/div>/s

匹配

Are you falling sick without any apparent illness? 
Your job could be the reason behind it. Check the list
of jobs that could jeopardize your health and send you to the doctor.

这是一个有效的演示:

http://regex101.com/r/pQ5eS7

答案 2 :(得分:0)

首先,您将\"与类属性旁边的"\混合在一起。

嗯,你没有提到你正在使用的编程语言,但我会将标记添加到工作多行:

/<div class=\"report-content\">(.*?)<\/div>/s

在我看来,也没有理由使用\s所以我用空格替换。