tags:

views:

206

answers:

4

Hi there, I'm new to regular expressions, and have no clue where to start, it's like a diff language to me. But I need one quick to accomplish a task.

I need to take

http://www.domain.com/folder1/folder2/file_path.txt

and get just

/folder1/folder2/file_path.txt

from it.

Thanks!

+4  A: 

construct a URI object from it and one of the properties of it will have what you want.

BCS
+2  A: 

I think that regex should work:

^http://.*?/(.*)$

(tested with Python)

Michał Niklas
not very generic but does answer the question.
BCS
A: 

Since VB.NET is in the tag for this question, I assume you have access at the server side to the Request object:

Dim instance As HttpRequest
Dim value As String

value = instance.Path

This should give you exactly what you asked for.


Edit: On second thought - you could be parsing URLs from some input string... in which case, regex will only help if you have a simple (regular) set of inputs:

Do you know all the possible domains? i.e. are "http://www.ABC.com" and "http://www.DEF.com" the only possible domains?

Then here:

Dim text As String = "http://www.ABC.com/folder1/folder2/file.txt"
Dim pattern As String = "(?:http://www.ABC.com|http://www.DEF.com)(.*)"

Dim r As Regex = new Regex(pattern, RegexOptions.IgnoreCase)

' Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
Dim g as Group = m.Groups(2)  'Gives the string matched by the capturing parentheses
Jeff Meatball Yang
A: 

Supporting more protocols and making the protocol optional too.

((https?|ftp)://)?(.*?)/(.*)
Bart J