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

本文关键字:提取 href ID 正则表达式 html | 更新日期: 2023-09-27 18:34:24

我试图提取 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值。

谢谢

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

请尝试这个

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