1

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.

Pratik
  • 30,557
  • 17
  • 84
  • 156
Pradeep
  • 4,252
  • 9
  • 37
  • 50

3 Answers3

8

If you just want everything before the query string:

^[^?]+
Petar Ivanov
  • 88,488
  • 10
  • 77
  • 93
4
Regex r = new Regex("(^[^?]+)");
Match m = r.Match("http://example.com/csd?/aaa/bbb");
// m.Groups[0].Value is your URL
sanmai
  • 26,245
  • 12
  • 59
  • 74
2

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.

yas4891
  • 4,664
  • 3
  • 32
  • 53