2016-02-13 49 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