有这样字符串:<li><a href="/q?ct=24&cm=16&tn=ucframework#ask" id="status-my-ask" target="_blank">我的提问</a></li><li><a href="/q?ct=24&cm=16&tn=ucframework#answer" id="status-my-answer" target="_blank">我的回答</a></li><li><a href="http://zhidao.baidu.com/q?ct=24&;cm=16&tn=ucframework#push" target="_blank">为我推荐的提问</a></li>从这些html源码中我只要抽取文本部分,就是说把所有的<>括号去掉,而且括号里面的连接也去掉...只要标签部分
结果: 我的提问 , 我的回答, 为我推荐的提问.
就这样..谢谢
结果: 我的提问 , 我的回答, 为我推荐的提问.
就这样..谢谢
var txt1 = GetElementById('status-my-ask').InnerText;
var txt2 = GetElementById('status-my-answer').InnerText;
txt1,txt2的值就是我的提问,我的回答。
第三个没有Id取的时候比较麻烦,可以用GetElementByTagName取到所有的a标签再遍历。最好还是设置一个id。。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
namespace sxLdfang
{
class Program
{
static void Main(string[] args)
{
string html = @"<li><a href=""/q?ct=24&cm=16&tn=ucframework#ask"" id=""status-my-ask"" target=""_blank"">我的提问</a></li><li><a href=""/q?ct=24&cm=16&tn=ucframework#answer"" id=""status-my-answer"" target=""_blank"">我的回答</a></li><li><a href=""http://zhidao.baidu.com/q?ct=24&;cm=16&tn=ucframework#push"" target=""_blank"">为我推荐的提问</a></li>";
string pattern = @"(?<=<li><a[^>]+>)[^<>]+(?=</a></li>)";
MatchCollection mc = Regex.Matches(html, pattern);
foreach (Match m in mc)
{
Console.WriteLine(m.Value);
}
Console.ReadKey();
}
}
}
运行结果:
我的提问
我的回答
为我推荐的提问
正则的话
string dest = Regex.Replace(yourHtml,@"<\w+\s[^>]*>",string.Empty);
http://hi.csdn.net/attachment/201202/13/309620_1329100367nxxg.jpg