Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get url from a text [duplicate]

Possible Duplicate:
regex for URL including query string

I have a text or message.

Hey! try this http://www.test.com/test.aspx?id=53

Our requirement is to get link from a text.We are using following code

List<string> list = new List<string>();
Regex urlRx = new
Regex(@"(?<url>(http:|https:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*)",
RegexOptions.IgnoreCase);

MatchCollection matches = urlRx.Matches(message);
foreach (Match match in matches)
{
   list.Add(match.Value);
}
return list;

It gives url but not the complete one.Output of the code is

http://www.test.com/test.aspx

But we need complete url like

http://www.test.com/test.aspx?id=53

Please suggest how to resolve that issue.Thanks in advance.

like image 688
PrateekSaluja Avatar asked Feb 03 '12 07:02

PrateekSaluja


People also ask

How do I extract a link from text?

Extract the Hyperlink URL with a Keyboard ShortcutSelect the cell containing the hyperlink and press Ctrl + K to open the Edit Hyperlink menu. This will open the Edit Hyperlink menu and you can copy and paste the URL from the Address just like before.


1 Answers

Try this regex, returns the query string also

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

You can test it on gskinner

like image 177
Amar Palsapure Avatar answered Oct 07 '22 05:10

Amar Palsapure