1

How I can find html element by id or class in string?(regex,C#)

<div class="item1">content</div>
<div class="item2">content</div>

get only

<div class="item1">
Evgeniy
  • 75
  • 1
  • 9
  • 12
    Use [HTML Agility Pack](http://htmlagilitypack.codeplex.com/), and [don't use regex](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Habib Nov 26 '12 at 12:36
  • any reason you don't want to user XmlDocument or XDocument instead of regex? – Arsen Mkrtchyan Nov 26 '12 at 12:37
  • 2
    Unless you are certain that the incoming HTML has a consistent format that will not change, [regex is a poor solution for parting HTML](http://stackoverflow.com/a/1732454/1583). As @Habib commented, use the HAP for this. – Oded Nov 26 '12 at 12:37
  • @ArsenMkrt - `XmlDocument` and `XDocument` will throw exceptions with well formed HTML (`
    ` is valid HTML, but not valid XML). A good option is the HAP which also deals with malformed documents and document fragments.
    – Oded Nov 26 '12 at 12:38
  • yes, but no idea from is the xml well formatted or not. In general I agree, Html Agile Pack is better for html – Arsen Mkrtchyan Nov 26 '12 at 12:40

1 Answers1

0

If you want to take only div - try this

<div (class|id)="myClassNameOrId".*?>

instead of myClassNameOrId input your own class or id name. If you want take other elements - replace div to other tag's name.

og Grand
  • 114
  • 3