自动识别超链接C#类

作者:互联网   出处:控件中国网   2014-11-05 19:21:37   阅读:1

自动识别超链接C#类

public string Url_Rewrite(string Text) 
    { 
        //用?正?则?表?达?式?识?别?URL超?链?接? 
        Regex UrlRegex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)", RegexOptions.IgnoreCase | RegexOptions.Compiled); 
        //进?规?则?查?询? 
        //Url 
        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)); 
        } 
        //用?正?则?表?达?式?识?别?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; 
    }

 

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat