ScottN
ScottN

Reputation: 1538

RegEx to get path of file, without domain

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!

Upvotes: 0

Views: 802

Answers (4)

Susheel Javadi
Susheel Javadi

Reputation: 3084

Supporting more protocols and making the protocol optional too.

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

Upvotes: 0

Jeff Meatball Yang
Jeff Meatball Yang

Reputation: 39057

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

Upvotes: 0

Michał Niklas
Michał Niklas

Reputation: 54342

I think that regex should work:

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

(tested with Python)

Upvotes: 2

BCS
BCS

Reputation: 78683

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

Upvotes: 4

Related Questions