自动识别超链接
/// <summary>
??? /// 自動(dòng)識(shí)別超鏈接
??? /// </summary>
??? /// <param name="Text"></param>
??? /// <returns></returns>
??? private string Url_自動(dòng)識(shí)別(string Text)//網(wǎng)絡(luò)上找到
??? {
??????? //用正則表達(dá)式識(shí)別URL超鏈接http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
??????? Regex UrlRegex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
??????? //用正則來查詢
???????
??????? MatchCollection matches = UrlRegex.Matches(Text);
??????? foreach (Match match in matches)
??????? {
??????????? Text = Text.Replace(match.Value, string.Format("<a href=\"{0}\" target=\"_blank\">{1}</a>", match.Value, match.Value));
??????? }
??????? //用正則表達(dá)式識(shí)別Email地址,和查詢超鏈接一樣
??????? Regex EmailRegex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
??????? matches = EmailRegex.Matches(Text);
??????? foreach (Match match in matches)
??????? {
??????????? Text = Text.Replace(match.Value, string.Format("<a href=mailto:{0}>{1}</a>", match.Value, match.Value));
??????? }
??????? return Text;
??? }
?
轉(zhuǎn)載于:https://www.cnblogs.com/shuyu/archive/2009/06/08/1499088.html
總結(jié)
- 上一篇: struts2+hibernate+sp
- 下一篇: 关于驴友、自助游!