0

How can I delete with regex from URL (string) http://test.com/org/category,1/2, this part:
/2 or /2/, with C# language

Of course, there will be numbers like category,1/412/, category,1/521, category,1/infinity..

How can I do this with regex?

4
  • 1
    that depends on what criteria you want to remove by. do you want to get rid of the last segment of a string which begins with '/'? Commented May 27, 2013 at 23:34
  • I want to delete segment/segments with int between /abc/, where abc is of course int type. Commented May 27, 2013 at 23:37
  • but, "infinity"? that's ... not really an int. Commented May 27, 2013 at 23:41
  • category,1/infinity => I mean very very large int. Commented May 27, 2013 at 23:42

1 Answer 1

4

It seems that you want to replace /\d+/?$ with empty string.

You could do that with:

url = Regex.Replace(url, @"/\d+/?$", "");
Sign up to request clarification or add additional context in comments.

3 Comments

Could you explain me step by step elements of this regex /\d+/?$ ? Thanks in advance :)
\d matches a "digit" character; + is equivalent to {1,}, one or more repetitions of the previous group. Thus, /, one or more digits, zero or one / (because of ?), end of line $.
@whoah, it's as sreservoir explained it, with minor correction, $ in this case matches end of string (or end of line at end of string).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.