简体   繁体   中英

Reg expression to include intranet sites that dont end in .com / .net / etc

I'm using the regexlib to come up with a regular expression to get a url out of a string.

So far I have the following:

    message = Regex.Replace(message, @"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+
([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?)", "<a target='_blank' 
href='$1'>$1</a>");

This seems to be pretty accurate but fails on two things:

  1. I can't get it to recognize a url if its of the form www.yahoo.com
  2. I can't get it to work for intranet sites so something like http://mysite/Views/Issues/ListView.aspx doesnt link :(

I despise regular expressions and could really use some help.

How about:

EDIT:

resultString = Regex.Replace(subjectString, @"(\b(?:(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&@#/%?=~_|$!:,.;]*[-A-Z0-9+&@#/%=~_|$]|((?:mailto:)?[A-Z0-9._%+-]+@[A-Z0-9._%-]+\.[A-Z]{2,6})\b)|""(?:(?:https?|ftp|file)://|www\.|ftp\.)[^""\r\n]+""|'(?:(?:https?|ftp|file)://|www\.|ftp\.)[^'\r\n]+')", "$1", RegexOptions.IgnoreCase | RegexOptions.Multiline);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM