Expressão regular para reconhecer url
Eu quero criar um Regex para url para obter todos os links da string de entrada. O Regex deve reconhecer os seguintes formatos do endereço de URL:
http (s): //www.webpage.cohttp (s): //webpage.co www.webpage.come também os URLs mais complicados, como: -http: //www.google.pl/#sclient=psy&hl=pl&site=&source=hp&q=regex+url&pbx=1&oq=regex+url&aq=f&aqi=g1&aql=&gs_sm=e&gs_upl=1582l30l5l0l0l0l0l0l0l0l0l0l0l0l0l0l0l0l0l0l0l0l0l0l0l0l0l0l0l0l de .r_gc.r_pw. & fp = 30a1604d4180f481 & biw = 1680 & bih = 935
Tenho o seguinte
((www\.|https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)
mas não reconhece o seguinte padrão: www.webpage.com. Alguém pode me ajudar a criar um Regex apropriado?
EDITAR Deverá encontrar um link apropriado e, além disso, colocar um link em um índice apropriado como este:
private readonly Regex RE_URL = new Regex(@"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)", RegexOptions.Multiline);
foreach (Match match in (RE_URL.Matches(new_text)))
{
// Copy raw string from the last position up to the match
if (match.Index != last_pos)
{
var raw_text = new_text.Substring(last_pos, match.Index - last_pos);
text_block.Inlines.Add(new Run(raw_text));
}
// Create a hyperlink for the match
var link = new Hyperlink(new Run(match.Value))
{
NavigateUri = new Uri(match.Value)
};
link.Click += OnUrlClick;
text_block.Inlines.Add(link);
// Update the last matched position
last_pos = match.Index + match.Length;
}