I need to strip out any “&id=SomeValue” from a Url.PathAndQuery. Where SomeValue could be an int or a string. And it may or may not be followed by another ampersand.
So it could be
somepage.aspx?cat=22&id=SomeId¶m2=4
or
somepage.aspx?cat=tect&id=450
I want to be left with
somepage.aspx?cat=22¶m2=4
or
somepage.aspx?cat=tect
Just going off the top of my head…
Update 2010-03-05 11:12 PM PST
I’ve been shamed by a comment into actually testing my code. What are you, my QA department? Here’s a working example using MSTest.
The regex, interpreted, says:
Then replace that expression with the backreference, so you don’t lose your initial ?/&.
note — as you can see from the tests, this emits a trailing ? or & when the replaced parameter is the only one or the last one, respectively. You could use string methods to get rid of that, though if somebody knows how to keep them out of the result using only regular expressions it would be excellent to see.