zoukankan      html  css  js  c++  java
  • 正则获取 某段 DIV 中 的内容

    string html = "<div class='aa'><div class='left'>324324<div>dsfsdf</div><h1>aa</h1></div></div>";
                // 获取第一个 相呼应的标记
                //Regex reg = new Regex(@"<div class='left'>([sS]+?)</div>");
                // 获取前后对应的标记
                Regex reg = new Regex(@"(?is)<div class='left'[^>]*>(?><div[^>]*>(?<o>)|</div>(?<-o>)|(?:(?!</?div).)*)*(?(o)(?!))</div>");
                // 获取第一条
                Match first = reg.Match(html);
                // 获取匹配到的所有集合
                MatchCollection list = reg.Matches(html);
                foreach (Match item in list)
                {
                    string value = item.Value;
                }

            public static string GetElementByClassName(string htmlConetnt, string label, string className)
            {
                Regex reg = new Regex(string.Format(@"(?is)<{0} class=""{1}""[^>]*>(?><{0}[^>]*>(?<o>)|</{0}>(?<-o>)|(?:(?!</?{0}).)*)*(?(o)(?!))</{0}>", label, className));
                Match first = reg.Match(htmlConetnt);
                return first.Value;
            }
    
            public static string GetElementById(string htmlConetnt, string label, string id)
            {
                Regex reg = new Regex(string.Format(@"(?is)<{0} id=""{1}""[^>]*>(?><{0}[^>]*>(?<o>)|</{0}>(?<-o>)|(?:(?!</?{0}).)*)*(?(o)(?!))</{0}>", label, id));
                Match first = reg.Match(htmlConetnt);
                return first.Value;
            }
    
            public static string GetLabel(string htmlConetnt, string label)
            {
                Regex reg = new Regex(string.Format(@"(?is)<{0}[^>]*>(?><{0}[^>]*>(?<o>)|</{0}>(?<-o>)|(?:(?!</?{0}).)*)*(?(o)(?!))</{0}>", label));
                Match first = reg.Match(htmlConetnt);
                return first.Value;
            }
  • 相关阅读:
    bzoj 3930: [CQOI2015]选数
    bzoj 2301: [HAOI2011]Problem b
    HDU 1695 GCD
    2017ACM/ICPC广西邀请赛-重现赛 1007.Duizi and Shunzi
    2017ACM/ICPC广西邀请赛-重现赛 1010.Query on A Tree
    2017ACM/ICPC广西邀请赛-重现赛 1004.Covering
    P3501 [POI2010]ANT-Antisymmetry
    P1171 售货员的难题
    P3385 【模板】负环
    P1659 [国家集训队]拉拉队排练
  • 原文地址:https://www.cnblogs.com/liuxiaoji/p/5969006.html
Copyright © 2011-2022 走看看