Andreaa
Andreaa

Reputation: 39

c# Regex inside some html tag

I'm trying during some hour with regex to take text inside some html tag:

<div class="ewok-rater-header-section">
  <ul class="header">

        <li><h1>meow</h1></li>

        <li><h1>meow2</h1></li>

        <li><h1>Time = <span class="work-weight">9.0 minutes</span></h1></li>

  </ul>
</div>

i take meow with

var regexpost = new System.Text.RegularExpressions.Regex(@"<h1(.*?)>(.*?)</h1>");
var mpost = regexpost.Match(reqpost);
string lechat = (mpost.Groups[2].Value).ToString();

but not other I like to add meow in a textbox , meow2 in a second textbox and 9.0 (minutes) in a last one

Upvotes: 3

Views: 90

Answers (2)

Andreaa
Andreaa

Reputation: 39

it s for parse http reponse. Then is it not slow to use a html parser to create document ?

Upvotes: 0

TheGeneral
TheGeneral

Reputation: 81583

In these situations a Html parser can help a lot, and can also be a lot more precise and robust

Html Agility pack

Example

var html = @"<div class=""ewok-rater-header-section"">
               <li><h1>meow</h1></li>
               <li><h1>meow2</h1></li>
               <li><h1>Time = <span class=""work-weight"">9.0 minutes</span></h1></li>
            </div>";

var doc = new HtmlDocument();
doc.LoadHtml(html);

// you can search for the heading
foreach (var node in doc.DocumentNode.SelectNodes("//li//h1"))
{
   Console.WriteLine("Found heading : " + node.InnerText);
}

// or you can be more specific
var someSpan = doc.DocumentNode
                  .SelectNodes("//span[@class='work-weight']")
                  .FirstOrDefault();

Console.WriteLine("Found span : " + someSpan.InnerText);

Output

Found heading : meow
Found heading : meow2
Found heading : Time = 9.0 minutes
Found span : 9.0 minutes

Demo here

Upvotes: 1

Related Questions