【问题标题】:Extract href ID in html page using regular expression使用正则表达式提取html页面中的href ID
【发布时间】:2016-02-13 13:15:49
【问题描述】:

我试图在 href 中提取 HTML 页面中的 ID。 Html 如下所示

<p>To register your account, please click the following link:</p>
<p><a href="https://abc-api-test.mywebsites.net:443/#/userreg/99978f1c-4c04-41ac-abcb-5039658a1f52" target="_blank">Complete registration.</a></p>
<p>If you have any questions please do not hesitate to contact us at <a href="mailto:muaccount@aol.net">

基本上我想从上面提取99978f1c-4c04-41ac-abcb-5039658a1f52 值。

谢谢

【问题讨论】:

    标签: c# html regex


    【解决方案1】:

    请试试这个

    // specify Regular expression
    Regex pageParser = new Regex(@"href=[""|']https://abc-api-test.mywebsites.net:443/#/userreg/(?<ID>[\S]*?)[""|']", RegexOptions.IgnoreCase | RegexOptions.Multiline);
    
    // extract matches from your HTML
    MatchCollection matches = pageParser.Matches(yourHtml);
    
    //Iterate through each match
    foreach (var m in matches)
    {
          var id = m.Groups["ID"].Value;
    
          // do whatever you want with the ID
    }
    

    【讨论】:

    • 如果需要将https://abc-api-test.mywebsites.net 作为参数传递,我该如何更改解决方案。我尝试了几个选项,但看起来这个格式化字符有点难。你能请。给他们一点指导。
    猜你喜欢
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 2011-12-11
    相关资源
    最近更新 更多