黑马程序员-黑马测试题
9、 给一段文本中匹配到的url添加超链接,比如把http://www.test.com替换为<a href="http://www.test.com"> http://www.test.com</a>。
namespace tes9
{
class Program
{
//9、 给一段文本中匹配到的url添加超链接,比如把http://www.test.com替换为<a href="http://www.test.com"> http://www.test.com</a>。
static void Main(string[] args)
{
string str = "我的一个网址为:http://www.test.com ";
//超链接正则
Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
//提取
string urlstr = urlregex.Match(str).Value;
//替换
str = urlregex.Replace(str, "<a href=" + urlstr + ">" + urlstr + "</a>");
Console.WriteLine(str);
Console.ReadKey();
}
}
}
posted on 2013-12-08 11:04 Efforts to achieve 阅读(222) 评论(0) 收藏 举报
浙公网安备 33010602011771号