Détecter et remplacer une URL dans un texte
Pour remplacer une URL ou un email dans un texte et le rendre cliquable :
message contient le texte.
string message_contenu = message.Replace("<br/>","\n\r");
try
{
Regex urlregex = new Regex(@"((http|https):\/\/([\w.]+\/?)\S*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
message_contenu = urlregex.Replace(message_contenu, "<a href=\"$1\" target=\"_blank\">$1</a>");
Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
message_contenu = emailregex.Replace(message_contenu, "<a href=mailto:$1>$1</a>");
}
catch
{
}
message_contenu = message_contenu.Replace("\n\r", "<br/>");
message contient le texte.
string message_contenu = message.Replace("<br/>","\n\r");
try
{
Regex urlregex = new Regex(@"((http|https):\/\/([\w.]+\/?)\S*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
message_contenu = urlregex.Replace(message_contenu, "<a href=\"$1\" target=\"_blank\">$1</a>");
Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
message_contenu = emailregex.Replace(message_contenu, "<a href=mailto:$1>$1</a>");
}
catch
{
}
message_contenu = message_contenu.Replace("\n\r", "<br/>");
Commentaires
Enregistrer un commentaire