• 正则获取 某段 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;
            }
  • 相关阅读:
    习题2-4 求交错序列前N项和 (15分)
    015_Web WPI
    014_捆绑包与显示模式
    013_模型验证
    012_模型绑定
    011_URL和Ajax辅助器方法
    010_模板辅助器方法
    009_辅助器方法
    008_视图
    007_控制器可扩展性
  • 原文地址:https://www.cnblogs.com/liuxiaoji/p/5969006.html
Copyright © 2020-2023  润新知