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

anubhava
  • 761,203
  • 64
  • 569
  • 643
user571188
  • 91
  • 1
  • 5
  • 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? – Daniel Brückner Apr 08 '14 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? – CAustin Apr 08 '14 at 17:45
  • @CAustin, && and ?& are leftovers from sloppy query part manipulation. – bzlm Apr 08 '14 at 17:48
  • possible duplicate of [Regular expression to remove one parameter from query string](http://stackoverflow.com/questions/1842681/regular-expression-to-remove-one-parameter-from-query-string) – bzlm Apr 08 '14 at 17:49
  • So would a trailing ? or & also be considered "sloppy" and therefore "wrong"? – ClickRick Apr 08 '14 at 17:52
  • @bzlm: I realize that, but the way he phrased the question, saying that he wanted to remove a certain string and leave the rest of the URL "intact" made me wonder if he wanted to keep that part for some reason. Kind of a dumb question on my part, but I just wanted to be sure. – CAustin Apr 08 '14 at 17:55
  • @CAustin I need to use the urls that are left when the parameter is stripped out. Like bzlm said, it looks sloppy and i don't think the search engines like it. I'm not even sure if && is valid. – user571188 Apr 08 '14 at 18:34
  • @DanielBrückner I'm not sure if I have to motivate my reasons? If I don't know how to code, I shouldn't try/ask for help? ... I think regexp looks like a is a great tool for this "small" (?) job. – user571188 Apr 08 '14 at 18:46
  • @user571188 It obviously is not a good tool for the job, otherwise it would have been easy to find a suitable expression. On the other hand there is, as you mentioned, a helper class for solving exactly this class of problems. – Daniel Brückner Apr 08 '14 at 18:52
  • @DanielBrückner That's your opinion. I prefer regexp for other reasons as well, such as performance and ease of porting. – user571188 Apr 08 '14 at 19:15

1 Answers1

1

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

Replace with: $1$2

CAustin
  • 4,525
  • 13
  • 25
  • Thanks. Looks a little bit weird in regexpr/regexpal, but does its job in .net. Would upvote this if i had the credentials. – user571188 Apr 09 '14 at 08:04