Che*_*hev 8 c# regex asp.net-mvc
可能重复:
用于链接字符串中URL的C#代码
我敢肯定这是一个愚蠢的问题,但我无法在任何地方找到合适的答案.我需要一个很好的URL正则表达式C#.它需要查找字符串中的所有URL,以便我可以将每个URL包装在html中以使其可单击.
用于此的最佳表达方式是什么?
一旦我有了表达式,用正确格式化的对应物替换这些URL的最佳方法是什么?
提前致谢!
Che*_*hev 40
我现在正在使用它:
text = Regex.Replace(text,
@"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)",
"<a target='_blank' href='$1'>$1</a>");
Run Code Online (Sandbox Code Playgroud)
使用此代码
protected string MakeLink(string txt)
{
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
MatchCollection mactches = regx.Matches(txt);
foreach (Match match in mactches)
{
txt = txt.Replace(match.Value, "<a href='" + match.Value + "'>" + match.Value + "</a>");
}
return txt;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
34849 次 |
最近记录: |