What is wrong with my regular expression?
How can i go abou开发者_JAVA百科t getting the value eg
<div class="detail"> Hello </div>
<div class="detail"> World </div>
string x = " <div class="results-list clearfix"> <div class="detail"> Hello </div> </div> <div class="results-list clearfix"> <div class="detail"> World </div> </div> "; String pattern = @"<div class=""results-list clearfix"">(?<Content>[^<]*)</div>"; Regex rx = new Regex(pattern,RegexOptions.Multiline); Match m = rx.Match(x); while (m.Success) { string zz = m.Groups["Content"].Value; m = m.NextMatch(); }
I think this is your problem ""results-list clearfix""
. As you are using a literal string, you can remove the extra "
's.
It is a bad idea to use regular expressions for this kind of parsing. Use an XML parser for this particular scenario. I suggest LINQ to XML, i.e. XElement.Parse(...)
Do not forget to wrap you html in a single root element though.
Try this pattern with SingleLine option:
string pattern = "<div\\sclass=\"results-list clearfix\">\\s*(?<Content><div[^>]*>.*?</div>)"
精彩评论