0

I'm trying to remove a query string parameter from a url using regular expressions (in .NET if it matters) without luck.

The solutions I've found on Stackoverflow use the HttpValueCollection, or similar helpers, or doesn't use the whole url including the ? before the query string. I would like this to work in plain regex if possible.

Parameter to remove: remove=me.

Sample urls:

http://{domain}/?remove=me
http://{domain}/?remove=me&foo=bar
http://{domain}/?baz=qux&remove=me&foo=bar
http://{domain}/?baz=qux&remove=me
http://{domain}/?baz=qux&tmpremove=me

After the parameter is removed the remaining url should be intact. No "&&", "?&", or "http://{domain}/&..." and it shouldn't touch the "tmpremove=me" :-)

Cheers

10
  • 2
    Why do you want to use regular expressions - a tool you don't understand well enough to use it for solving your problem - when there is already a working solution not involving regular expressions? Commented Apr 8, 2014 at 17:30
  • What do you mean when you say the URL should be intact but no "&&, "?&", etc? Do you want to keep those symbols or not? Commented Apr 8, 2014 at 17:45
  • @CAustin, && and ?& are leftovers from sloppy query part manipulation. Commented Apr 8, 2014 at 17:48
  • possible duplicate of Regular expression to remove one parameter from query string Commented Apr 8, 2014 at 17:49
  • So would a trailing ? or & also be considered "sloppy" and therefore "wrong"? Commented Apr 8, 2014 at 17:52

1 Answer 1

1

Match: (?:[?&]remove=me$)|(?:([?&])remove=me&(.+))

Replace with: $1$2

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Looks a little bit weird in regexpr/regexpal, but does its job in .net. Would upvote this if i had the credentials.

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.