在作出明文字符串的正则表达式问题的URL点击 [英] Regex string issue in making plain text urls clickable

查看:191
本文介绍了在作出明文字符串的正则表达式问题的URL点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个工作正则表达式code在C#中检测纯文本的URL(HTTP / HTTPS / FTP / FTPS)的字符串,并让他们点击通过把一个锚标记周围有相同的URL。我已经做了一个正则表达式模式和code为附后。

I need a working Regex code in C# that detects plain text urls (http/https/ftp/ftps) in a string and make them clickable by putting an anchor tag around it with same url. I have already made a Regex pattern and the code is attached below.

但是,如果已经有任何可点击的网址是present输入字符串那么上面的code提出另一个锚标记权。例如现有子在下面的code:字符串sContent:ftp://www.abc.com> FTP ://www.abc.com 拥有它的另一个锚标记低于code运行时。有什么办法解决?

However, if there is already any clickable url is present in the input string then the above code puts another anchor tag over it. For example the existing substring in the below code: string sContent: "ftp://www.abc.com'>ftp://www.abc.com" has another anchor tag over it when the code below is run. Is there any way to fix it?

        string sContent = "ttt <a href='ftp://www.abc.com'>ftp://www.abc.com</a> abc ftp://www.abc.com abbbbb http://www.abc2.com";

        Regex regx = new Regex("(http|https|ftp|ftps)://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);

        MatchCollection mactches = regx.Matches(sContent);

        foreach (Match match in mactches)
        {
            sContent = sContent.Replace(match.Value, "<a href='" + match.Value + "'>" + match.Value + "</a>");
        }

另外,我希望有一个正则表达式code键使邮件的点击与电子邮件地址标签。我可以做我自己,但双锚标记的上述问题也将出现在其中。

Also, I want a Regex code to make emails as clickable with "mailto" tag. I can do it myself but the above mentioned issue of double anchor tag will also appear in it.

推荐答案

我注意到你的榜样测试字符串,如果一个重复的链接,例如 ftp://www.abc.com 中的字符串,并已链接,那么结果将是双锚链接。王家军前pression你已经拥有和@stema已提供将工作,但你需要接近你如何更换匹配的 sContent 不同的变量。

I noticed in your example test string that if a duplicate link e.g. ftp://www.abc.com is in the string and is already linked then the result will be to double anchor that link. The Regular Expression that you already have and that @stema has supplied will work, but you need to approach how you replace the matches in the sContent variable differently.

你想要什么下code的例子应该给你:

The following code example should give you what you want:

string sContent = "ttt <a href='ftp://www.abc.com'>ftp://www.abc.com</a> abc ftp://www.abc.com abbbbb http://www.abc2.com";

Regex regx = new Regex("(?<!(?:href='|<a[^>]*>))(http|https|ftp|ftps)://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);

MatchCollection matches = regx.Matches(sContent);

for (int i = matches.Count - 1; i >= 0 ; i--)
{
    string newURL = "<a href='" + matches[i].Value + "'>" + matches[i].Value + "</a>";

   sContent = sContent.Remove(matches[i].Index, matches[i].Length).Insert(matches[i].Index, newURL);
}

这篇关于在作出明文字符串的正则表达式问题的URL点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆