2

I have this url pattern

http://dev.virtualearth.net/REST/v1/Locations?
addressLine={0}&
adminDistrict={1}&
locality={2}&
countryRegion={3}&
postalCode={4}&
userLocation={5}&
inclnb=1&
key={6}  

Let us say that locality and userLocation have no values

http://dev.virtualearth.net/REST/v1/Locations?
addressLine=Main&
adminDistrict=WA&
locality=&
countryRegion=US&
postalCode=98001&
userLocation=&
inclnb=1&
key=BingKey  

Then I want to remove all parameters that is equal to "&"
Like for example: 'locality=&' and 'userLocation=&'

And should be look like this:

http://dev.virtualearth.net/REST/v1/Locations?
addressLine=Main&
adminDistrict=WA&
countryRegion=US&
postalCode=98001&
inclnb=1&
key=BingKey  

Final Output:

http://dev.virtualearth.net/REST/v1/Locations?addressLine=Main&adminDistrict=WA&countryRegion=US&postalCode=98001&inclnb=1&key=BingKey  
7
  • Who's to say they are in order? Commented Aug 22, 2013 at 14:05
  • What do you mean? I know that this is possible in regex. I just don't know how to do it. Commented Aug 22, 2013 at 14:06
  • Who's to say that the parameters are in that order? You really should be using a URL parser. Commented Aug 22, 2013 at 14:09
  • Are you building the URL yourself or do you not have any control over it? Commented Aug 22, 2013 at 14:09
  • 1
    @fiberOptics In that case do a !string.IsNullOrEmpty check on each of the parameters before adding them to the URL and skip a regex. Commented Aug 22, 2013 at 14:18

2 Answers 2

4

Why do you specificly want to use regular expressions? There are some classes in C# specificly build for building and handling URIs. I suggest you look at HttpUtility.ParseQueryString() or Uri.TryCreate.

You would then parse the query string, loop through the variables that have only a key and no value, and reconstruct a new uri without them. It will be much easier to read and maintain than a regular expression.


Edit: I quickly decided to see how this could be done:

string originalUri = "http://www.example.org/etc?query=string&query2=&query3=";

// Create the URI builder object which will give us access to the query string.
var uri = new UriBuilder(originalUri);

// Parse the querystring into parts
var query = System.Web.HttpUtility.ParseQueryString(uri.Query);

// Loop through the parts to select only the ones where the value is not null or empty  
var resultQuery = query.AllKeys
                       .Where(k => !string.IsNullOrEmpty(query[k]))
                       .Select(k => string.Format("{0}={1}", k, query[k]));

// Set the querystring part to the parsed version with blank values removed
uri.Query = string.Join("&",resultQuery);

// Done, uri now contains "http://www.example.org/etc?query=string"
Sign up to request clarification or add additional context in comments.

Comments

2

@"[\w]+=\&" should get you what you are looking for, but wouldn't it be easier to simply not add the parameter to the url string if the corresponding value is empty?

1 Comment

Thank you for the answer. I like your suggestion but I think Willem answer is the right way to do. +1

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.