Pradeep
Pradeep

Reputation: 4872

Regular expression to get Url without Query string

I am looking for a way to parse URL without queryString using Regular expression.

i have url like "http://abc.com/csd?/aaa/bbb"

expected is like "http://abc.com/csd"

Anybody help me on this.

Upvotes: 2

Views: 11945

Answers (3)

sanmai
sanmai

Reputation: 30951

Regex r = new Regex("(^[^?]+)");
Match m = r.Match("http://example.com/csd?/aaa/bbb");
// m.Groups[0].Value is your URL

Upvotes: 4

yas4891
yas4891

Reputation: 4862

You could use Substring and IndexOf

var someString = "http://abc.com/csd?/aaa/bbb";
someString.Substring(0, someString.IndexOf('?') - 1);

while this does not fully comply with the requirements stated in your question, it might be an easier approach - if actual implementation does not need to be RegEx.

Upvotes: 2

Petar Ivanov
Petar Ivanov

Reputation: 93090

If you just want everything before the query string:

^[^?]+

Upvotes: 12

Related Questions