开发者

Extract Content from Div Tag C# RegEx

开发者 https://www.devze.com 2023-02-06 12:39 出处:网络
I need to extract this content inside the divtestimonial1 div I am using the following regEx, but its only returning the first line

I need to extract this content inside the divtestimonial1 div I am using the following regEx, but its only returning the first line

Regex r = new Regex("&lt;div([^<]*<(?!/div>))");
  <div class="开发者_如何学运维testimonial_content" id="divtestimonial1">
          <a name="T1"></a>
          <div class="testimonial_headline">%testimonial1headline</div>
          <p align="left"><img src="" alt="" width="193" height="204" align="left" hspace="10" id="img_T1"/><span class="testimonial_text">%testimonial1text</span><br />
          </p>
  </div>


Regular expressions are generally not a good choice for parsing HTML. You might be better off using a tool such as HTML Agility Pack, so I would suggest you use that.

That being said, you can match your particular sample input using this Regex:

<div.*?id="divtestimonial1".*?>.*</div>

But it might break in your real-world scenario. One of the troubles with Regex and HTML is properly detecting nesting of tags, etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消