欢迎访问张朋的技术分享社区
当前位置: 首页 > 技术分享  > C#

正则获取元素内容

2019/3/4 15:13:22 人评论

public static string GetElementByClassName(string htmlConetnt, string label, string className) { Regex reg = new Regex(string.Format(@"(?is)<{0} class=""{1}""[^>]*>(?><{0}[^>]*>(?<o>)|</{0}&…

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}\b).)*)*(?(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}\b).)*)*(?(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}\b).)*)*(?(o)(?!))</{0}>", label));

      Match first = reg.Match(htmlConetnt);

      return first.Value;

   }

 var res2 = GetElementByClassName(responseString2, "div", "listmain");

Regex reg2 = new Regex(@"<a[^>]*href=([""'])?(?<href>[^'""]+)\1[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase);

                Match mc2 = reg2.Match(res2);

                if (mc2.Success)

                {

                    string content = mc2.Groups["href"].Value.Trim();

                }


附件下载

相关技术

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?