简体   繁体   English

如何在C#中的div标签之间获取内容

[英]How to get content between the div tags in C#

Code which wants to extract data: 想要提取数据的代码:

<div class="Content">
    <div>
        <h3>Opening hours:</h3>

        <div>I want to get this text</div>

    </div>
</div>

I tried with this code: 我尝试使用以下代码:

Match OpeningHours = Regex.Match(data, "<h3>Opening hours:</h3>\n<div>(.+?)</div>");
if (OpeningHours.Success)
{
     string nOpeningHours = OpeningHours.Groups[1].Value;
     company.OpeningHours = nOpeningHours;
}

You need to use \\s* after \\n , so that the spaces or even line breaks after the </h3>\\n got matched. 您需要在\\n之后使用\\s* ,以使</h3>\\n匹配后的空格甚至是换行符。 \\s matches any kind of vertical or horizontal white space character. \\s匹配任何类型的垂直或水平空白字符。

Regex.Match(data, @"<h3>Opening hours:</h3>\n\s*<div>(.+?)</div>");

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM