1

I need to get version no part from a url. Url will look like this "http://myweb.com/api/v1/customer". I need to get the "1" value from the url. How can i do this?

Thanks in advance

3
  • So what is the pattern? Get the only number which is in the url? Commented Dec 22, 2014 at 5:57
  • I need to get the version no. The url is always "/api/v*/". I need to get the "v1" value or just the "1" Commented Dec 22, 2014 at 6:00
  • if version can be anywhere in url then api/v(.+)/ regex will capture version in group \1 Commented Dec 22, 2014 at 6:01

5 Answers 5

7

You can use the Uri class, which has a built in parser specifically for parsing uris, and exposes a nice API for examining the components of the URI.

Uri uri = new UriBuilder("http://myweb.com/api/v1/customer").Uri;
string versionString = uri.Segments[2]; // v1/

You can, of course, further process this to extract just the number, as shown in the next snippet. The benefit is that you won't have to worry about complicated edge cases in parsing URIs with your regex.

int version = int.Parse(Regex.Match(versionString, @"\d+").Value);

Here is a demonstration: http://ideone.com/4kgey7

Sign up to request clarification or add additional context in comments.

4 Comments

Does it work if the url is "www.myweb.com/api/v1/customer" or "myweb.com/api/v1/customer"?
@Reynaldi Not with the URI constructor. If you want fault tolerant URI parsing, you can use new UriBuilder(yourURIString).Uri;
\d+ would return any number not only the one next to /v
@AvinashRaj Yes, but since we've already extracted only the v1/ segment of the URI, we can use a naive regex like \d+ against the extracted URI segment. If you've spotted a case where this code works incorrectly, please fork the demo and change the string to demonstrate the problem.
2

You could use lookaround assertions like below,

Regex.Match(yourstring, @"(?<=/v)\d+(?=/)").Value;
  • (?<=/v) Positive lookbehind asserts that the match must be preceded by /v
  • \d+ Matches one or more digits.
  • (?=/) Positive lookahead asserts that the match must be followed by a forward-slash / character.

DEMO

1 Comment

Thanks for your answer. Your code looks more simple. I modify your code a bit to "Regex.Match(yourstring, @"(?<=/api/v)\d+(?=/)").Value;"
1

If you are using MVC you can use attribute routing:

[GET("api/v{version:int}/customer")]
public ActionResult GetCustomer(int version) {
...

https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing

Comments

0
(?<=v)\d+

You can use this simple regex to do that.See demo.

https://regex101.com/r/vN3sH3/36

Comments

-1
string line = "http://myweb.com/api/v1/customer"
string[] arr = line.Split('\');

foreach(string str in arr)
{
    if(str[0] == 'v')
    {
       int v = Convert.ToInt32(str.SubString(1));
    }
}

Comments

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.