我有一个看起来像这样的字符串:
<a href="http://forum.tibia.com/forum/?action=board&boardid=476">Amera</a><br><font class="ff_info">This board is for general discussions related to the game world Amera.</font>
如何忽略/删除</a>
之后的所有内容,然后只获取网址:http://forum.tibia.com/forum/?action=board&boardid=476
和值Amera
所以之后,我想要2个变量及其值,如:
string url = "http://forum.tibia.com/forum/?action=board&boardid=476";
和
string value = "Amera";
我试过这个来获得价值:
string value = System.Text.RegularExpressions.Regex.Replace(MYSTRING, "(<[a|A][^>]*>|)", "");
但它返回:
Amera</a><br><font class="ff_info">This board is for general discussions related to the game world Amera.</font>
答案 0 :(得分:0)
要获取网址,请尝试使用此正则表达式模式:/href=\"(.*)\"/
...并获取 > Amera </a>
之间的值
使用类似:>(.+?)</a>
......虽然,这似乎远非完美......
答案 1 :(得分:0)
如果a
标记不包含更多属性,则只能将其用于网址:
\bhref="(.*?)"
对于网址和文字来说更复杂:
<a\b[^>]*?\bhref="([^"]*?)"[^>]*?>(.*?)<\/a>
所以在C#代码中(引号需要转义!):
var html = "<a href=\"http://forum.tibia.com/forum/?action=board&boardid=476\">Amera</a><br><font class=\"ff_info\">This board is for general discussions related to the game world Amera.</font>";
var match = Regex.Match(html, "<a\\b[^>]*?\\bhref=\"([^\"]*?)\"[^>]*?>(.*?)<\\/a>", RegexOptions.IgnoreCase);
if (match.Success) {
var url = match.Groups[1];
var text = match.Groups[2]
}
答案 2 :(得分:0)
试试这个:
HtmlDocument dc = new HtmlAgilityPack.HtmlDocument();
dc.LoadHtml("<a href='http://forum.tibia.com/forum/?action=board&boardid=476'>Amera</a><br><font class='ff_info'>This board is for general discussions related to the game world Amera.</font>");
foreach (HtmlNode link in dc.DocumentNode.SelectNodes("a"))
{
string url = link.Attributes["href"].Value; // http://forum.tibia.com/forum/?action=board&boardid=476
string value = link.InnerText; // Amera
}