How can I extract a valid URL from a string like this one
h*tps://www.google.com/url?q=h*tp://www.site.net/file.doc&sa=U&ei=_YeOUc&ved=0CB&usg=AFQjCN-5OX
I want to extract this part: h*tp://www.site.net/file.doc
, this is my valid URL.
How can I extract a valid URL from a string like this one
h*tps://www.google.com/url?q=h*tp://www.site.net/file.doc&sa=U&ei=_YeOUc&ved=0CB&usg=AFQjCN-5OX
I want to extract this part: h*tp://www.site.net/file.doc
, this is my valid URL.
Add System.Web.dll assembly and use HttpUtility class with static methods. Example:
using System;
using System.Web;
class MainClass
{
public static void Main (string[] args)
{
Uri uri = new Uri("https://www.google.com/url?q=http://www.site.net/file.doc&sa=U&ei=_YeOUc&ved=0CB&usg=AFQjCN-5OX");
Uri doc = new Uri (HttpUtility.ParseQueryString (uri.Query).Get ("q"));
Console.WriteLine (doc);
}
}
h*tps
, would Uri
parse the string correctly?
May 13, 2013 at 10:42
Invalid URI: The URI scheme is not valid.
exception.
May 13, 2013 at 15:36
h*tps
is not a valid protocol so how could it parse it?
May 13, 2013 at 15:59
I don't know what your other strings can look like, but if your 'valid URL' is between the first =
and the first &
, you could use:
(?<==).*?(?=&)
It basically looks for the first =
and matches anything before the next &
.
Tested here.
You can use split
function
string txt="https://www.google.com/url?q=http://www.site.net/file.doc&sa=U&ei=_YeOUc&ved=0CB&usg=AFQjCN-5OX";
txt.split("?q=")[1].split("&")[0];
in this particular case with the string you posted you can do this:
string input = "your URL";
string newString = input.Substring(36, 22) ;
But if the length of the initial part of the URL changes, and also the lenght of the part you like to extract changes, then would not work.