使用正则表达式在html页面中提取href ID

时间:2016-02-13 13:15:49

标签: c# html regex

我正在尝试提取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值。

由于

1 个答案:

答案 0 :(得分:2)

请试试这个

// 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
}