开发者

What is wrong with my regular expression?

开发者 https://www.devze.com 2023-02-09 11:48 出处:网络
How can i go abou开发者_JAVA百科t getting the value eg <div class=\"detail\">Hello</div>

How can i go abou开发者_JAVA百科t getting the value eg

  1. <div class="detail"> Hello </div>
  2. <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>)"
0

精彩评论

暂无评论...
验证码 换一张
取 消