2

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.

5
  • This is a new or unique kind of regular expression? Commented Sep 28, 2011 at 5:52
  • 2
    Couldn't you use url.Split('?')[0] Commented Sep 28, 2011 at 5:53
  • System.Uri class has methods that will help you in doing such thing without regex. Commented Sep 28, 2011 at 5:54
  • Using a regex is probably not the best way to go, unless you're going to expand upon the use. I would simply use String.Substring(...) and String.IndexOf(...) for this. Regex is uber-slow by comparison. Commented Sep 28, 2011 at 5:55
  • When you have exact char that is delimiting your string its always better to use string methods. Regular expressions are for more advanced cases. Commented Sep 28, 2011 at 5:55

3 Answers 3

12

If you just want everything before the query string:

^[^?]+
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.