0

Hi I'm very new to regex and I need some help with writing this or at least getting me started.

I would like to get all the divs on the page and put them into a string collection

there might be spaces between the < and div and a spaces between the < / div > thanks

I have tried the htmlaggilitypack but was experiencing issues thats why I am going this way

Dim reg As Regex = New Regex("<div(.*?)> </div")

Dim matches As string() = reg.Matches(htmlCode)




<div id="out">

    <div id="one">
        < div id="b"></div>
        <   div id="d"></div>
    </div>

    <div     id="two">
        <h1>fsdfsdf</h1>
        <  div id="a"><div id="a"></div></div>
    < /  div  >

</div>
just.another.programmer
  • 8,579
  • 8
  • 51
  • 90
Hello-World
  • 9,277
  • 23
  • 88
  • 154

2 Answers2

2

If you want to return a collection of divs by an ID value then you could use the following with HMTL agility pack:

protected void Page_Load(object sender, EventArgs e)
{
     List<HtmlAgilityPack.HtmlNode> divs = GetDivsInner();

     foreach (var node in divs)
     {
          Response.Write("Result: " + node.InnerHtml.ToString());
     }

}

public List<HtmlAgilityPack.HtmlNode> GetDivsInner()
{
      HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

      doc.OptionFixNestedTags = true;
      doc.Load(requestData("YOUR URL HERE"));

      var divList = doc.DocumentNode.Descendants("div").Where(d => d.Attributes.Contains("id") && d.Attributes["id"].Value.Contains("YOUR ID VALUE")).ToList();

      return divList;
}

public StreamReader requestData(string url)
{
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
      HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

      StreamReader sr = new StreamReader(resp.GetResponseStream());

      return sr;
}
dtsg
  • 4,408
  • 4
  • 30
  • 40
1

Try

<\s*div.*>(.|\n)*<\s*/\s*div>  

as your regex pattern. Have tested it with the following and it matches all

<div id='d'>
  dsfdsfs

  dsfdfd

</div>
< div >dave </div>
<div>home </ div>
<p></p>

however if you want to tweak it there are some great tools on the internet to test your regular expressions

http://www.regextester.com/

http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx

Cylian
  • 10,970
  • 4
  • 42
  • 55
Mingo
  • 836
  • 3
  • 12
  • 19