1

There are some addresses as follows :

http://rs320tl.rapidshare.com/files/119371167/sth.rar

I'm gonna select rs320tl.rapidshare.com with Regex, but I'm not familiar with Regular Expressions.
Would you please guide me ?

Thanks.

PS.
rs320tl in the address is variable.

3 Answers 3

4

You should not use regular expressions to do this.

Instead, use the Uri class:

Uri uri = new Uri(yourString, UriKind.Absolute);
string host = uri.Host;

If you want to check whether the string is acutally a URL, use the following code:

Uri uri;
if (!Uri.TryCreate(yourString, UriKind.Absolute, out uri))
    //String is not a valid URL.  Waah waah waah
string host = uri.Host;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but what about the Regex one, Would you please post a regular expression to do that.
Are you sure! it selects whole of the address!
You need to use the capture group.
3

If you really want to go down the Regex/C# route, I think what you are looking for is something like this:

string sOriginalUrl = "http://rs320tl.rapidshare.com/files/119371167/sth.rar";
string sPattern = "http://(?'host'[0-9a-zA-Z-.]*)/.*";
Regex re = new Regex(sPattern, RegexOptions.ExplicitCapture);
string sHost = re.Match(sOriginalUrl).Groups["host"].Value;

Comments

1

"//(\w.*?\w)/" group[1] will have your url

2 Comments

Are you sure about the expression! I wanted to test it, I receive this error Unrecognized escape sequence for w
You need to put an @ before the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.