I haven't used regex much before but found something useful on the net that I'm using:
private string ConvertUrlsToLinks(string msg)
{
string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\\||\# |!|\(|?|\[|,| |>|<|;|\)])";
Regex r = new Regex(regex, RegexOptions.IgnoreCase);
return r.Replace(msg, "<a href=\"$1\" title=\"Click to open in a new window or tab\" target=\"_blank\">$1</a>").Replace("href=\"www", "href=\"http://www").Replace(@"\r\n", "<br />").Replace(@"\n", "<br />").Replace(@"\r", "<br />");
}
It does a good job but now I want it to exclude urls that already have a "a href=" in front. There's the ending "/a" to consider too.
Can that be done with regex or have to use totally different approach, like coding?
Try this:
((?<!href=')(?<!href=")(www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\\||\# |!|\(|?|\[|,| |>|<|;|\)])
I tested on regex101.com
With the following sample set:
www.google.com
http://hi.com
http://www.fishy.com
href='www.ignore.com'
www.ouch.com
Using your existing regex pattern you could make a few simple changes to handle additional text being prepended or appended to your string:
`.+` <- pattern -> `(.+)?`
Which would give you:
.+((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\\||\# |!|\(|?|\[|,| |>|<|;|\)])(.+)?
So passing the string of either:
<a href='http://www.test.com'>http://www.test.com</a>
...or...
http://www.test.com
Would result in:
<a href="http://www.test.com" title="Click to open in a new window or tab" target="_blank">www.test.com</a>
Examples:
I think it would be a little ToNy tHe pOny to do that in regex after all, so wrote the code, in case anyone is interested here it is:
private string handleatag(string msg, string tagbegin, string tagend)
{
ArrayList tags = new ArrayList();
int tagbeginpos = msg.IndexOf(tagbegin);
int tagendpos;
string hash = tagbegin.GetHashCode().ToString();
while (tagbeginpos != -1)
{
tagendpos = msg.IndexOf(tagend, tagbeginpos);
if (tagendpos != -1)
{
string atag = msg.Substring(tagbeginpos, tagendpos - tagbeginpos + tagend.Length);
msg = msg.Replace(atag, hash + tags.Count.ToString());
tags.Add(atag);
}
else
msg = msg.Remove(tagbeginpos, tagbegin.Length);
tagbeginpos = msg.IndexOf(tagbegin, tagbeginpos);
}
msg = ConvertUrlsToLinks(msg);
for (int i = 0; i < tags.Count; i++)
msg = msg.Replace(hash + i.ToString(), tags[i].ToString());
return msg;
}
private string ConvertUrlsToLinks(string msg)
{
if (msg.IndexOf("<a href=") != -1)
return handleatag(msg, "<a href=", "</a>");
string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\\||\# |!|\(|?|\[|,| |>|<|;|\)])";
Regex r = new Regex(regex, RegexOptions.IgnoreCase);
return r.Replace(msg, "<a href=\"$1\" title=\"Click to open in a new window or tab\" target=\"_blank\">$1</a>").Replace("href=\"www", "href=\"http://www").Replace(@"\r\n", "<br />").Replace(@"\n", "<br />").Replace(@"\r", "<br />");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.