2016-02-13 104 views
0

我试图提取href内HTML页面中的ID。 HTML看起来像下面使用正则表达式在html页面中提取href ID

<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:[email protected]"> 

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

感谢

回答

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 
} 
+0

如果你需要传递''https://abc-api-test.mywebsites.net作为参数,我怎样才能改变溶液。我尝试了几个选项,看起来像这个格式化字符,但它有点艰难。你可以套住吗?给他们一点指导。 – SMPH