【发布时间】:2010-11-13 16:19:45
【问题描述】:
我正在尝试从 href 中的 url 参数获取 ID,如下所示:
<a href="http://www.mysite.com/myitem.php?id=71312">MyItemName</a>
我只想要 71312,目前我正在尝试使用正则表达式(但如果你有更好的方法,我很乐意尝试):
string html,itemID;
using (var client = new WebClient())
{
html = client.DownloadString("http://www.mysite.com/search.php?search_text=" + myItemName);
}
string pattern = "<a href=\"http://www.mysite.com/myitem.php?id=(\d+)\">" + myItemName + "</a>";
Match m = Regex.Match(html, pattern, RegexOptions.IgnoreCase);
if (m.Success)
{
itemID = m.Groups[1].Value;
MessageBox.Show(itemID);
}
html 示例:
more html body
<h1>Items - List</h1>
<p><a href="http://www.mysite.com/myitem.php?id=12313">MyItemNameTest</a>, <a href="http://www.mysite.com/myitem.php?id=83">MyItemNameTestB</a>, <a href="http://www.mysite.com/myitem.php?id=213784">MYItemNameOther</a></p>
</div>
more html body
【问题讨论】:
标签: c# regex html-parsing