正则表达式来获得HREF的链接。 [asp.net] [英] Regex to get the link in href. [asp.net]

查看:210
本文介绍了正则表达式来获得HREF的链接。 [asp.net]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有麻烦让我的正则表达式来工作。 IM使用C#asp.net工作
我将张贴code我现在用的什么我不能让工作是第二个正则表达式来获得无论是在HREF =LINK

THX提前

  VAR =的TextBodyLorem存有...< A HREF =HTTP://www.link.com'>链接< / A>中;
        VAR urlTagPattern =新的正则表达式(@<?一个* HREF ='](小于?URL方式>?*)[,'] *>?(LT;名称> *? )所述; / A>中,RegexOptions.IgnoreCase);        //这是正则表达式
        VAR hrefPattern =新的正则表达式(@HREF = {:Q} \\>中,RegexOptions.IgnoreCase);
        VAR的url = urlTagPattern.Matches(的TextBody);
        的foreach(URL中匹配的网址)
        {            变种的HREF = hrefPattern.Match(url.ToString());
            litStatus.Text = hrefs.ToString();
        }


解决方案

欢迎您日常的分期付款的不使用正则表达式来解析HTML 的。在这个版本中的不使用正则表达式来解析HTML 的,我们会提醒你不要使用正则表达式来解析HTML,因为HTML不能可靠地通过正则表达式解析和几十个有效的HTML构造将打破提出了天真的正则表达式。我们不会提及所有常用的附加无效问卷网页上的不使用正则表达式来解析HTML 的今天。

另外在不要使用正则表达式来解析HTML 的,我们将链接到的Html敏捷性包,你可以用它来正确地解析HTML,随后在短短code(一个非常类似的例子是在该网页上present)的行可靠提取链接URL的.NET库。

我们希望你喜欢今天的不使用正则表达式来解析HTML 的,并期待明天再见到你的另一个令人兴奋的版本的不使用正则表达式来解析HTML 的,当有人张贴关于使用正则表达式解析HTML另一个问题。但是,从是所有的不使用正则表达式来解析HTML 的现在。再见!

hi i got problems to get my regex to work. im working with C# asp.net i will post the code i use now and what i cant get to work is the second regex to get whatever is in the href="LINK"

thx in advance

var textBody = "lorem ipsum... <a href='http://www.link.com'>link</a>";


        var urlTagPattern = new Regex(@"<a.*?href=[""'](?<url>.*?)[""'].*?>(?<name>.*?)</a>", RegexOptions.IgnoreCase);



        //THIS IS THE REGEX
        var hrefPattern = new Regex(@"HREF={:q}\>", RegexOptions.IgnoreCase);




        var urls = urlTagPattern.Matches(textBody);


        foreach (Match url in urls)
        {

            var hrefs = hrefPattern.Match(url.ToString());


            litStatus.Text = hrefs.ToString();
        }

解决方案

Welcome to your daily installment of Don't Use Regex To Parse HTML. In this edition of Don't Use Regex To Parse HTML, we'll be reminding you not to use regex to parse HTML because HTML cannot reliably be parsed by a regex and dozens of valid HTML constructs will break the naïve regex proposed. We won't be mentioning all the additional invalid ones in common use on the web in Don't Use Regex To Parse HTML today.

Also in Don't Use Regex To Parse HTML, we'll be linking to the Html Agility Pack, a .NET library you can use to parse HTML properly and subsequently extract link URLs reliably in just a couple of lines of code (a very similar example being present on that page).

We hope you have enjoyed today's Don't Use Regex To Parse HTML, and look forward to seeing you again tomorrow for another exciting edition of Don't Use Regex To Parse HTML, when someone posts another question about using regex to parse HTML. But that's all from Don't Use Regex To Parse HTML for now. Bye!

这篇关于正则表达式来获得HREF的链接。 [asp.net]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆