3

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

ElasticCode
  • 7,311
  • 2
  • 34
  • 45
Andreaa
  • 39
  • 2
  • 3
    Although regex could easily solve this problem in this specific circumstance. It truely is not the right tool for the job. if you need to parse html, its best to use a tool that understands html and can easily process it. like html agility pack... http://html-agility-pack.net/ i – TheGeneral Apr 14 '18 at 01:58
  • (?![\s]|.*=.*)(?<=\>)([^><]+)(?=\<) – Mohammed Elhag Apr 14 '18 at 02:01
  • 2
    https://stackoverflow.com/a/1732454/3010968 – Selman Genç Apr 14 '18 at 02:08

2 Answers2

1

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

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

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

Andreaa
  • 39
  • 2