.NET Regular expression for querystring value

.net, c#, regex

Solution

Just going off the top of my head...

string url = "somepage.aspx?cat=22&id=SomeId&param2=4";
Regex regex = new Regex("([\?\&])id=[^\?\&]+");
url = regex.replace(url, "\1");
System.Diagnostics.Debug.WriteLine("url = " + url);

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.

    Regex regex = new Regex(@"([\?\&])id=[^\&]+[\&]?");

    [TestMethod]
    public void RegexReplacesParameterInMiddle()
    {
        string url = "somepage.aspx?cat=22&id=SomeId&param2=4";
        url = regex.Replace(url, "$1");
        Assert.AreEqual("somepage.aspx?cat=22&param2=4",url);
    }

    [TestMethod]
    public void RegexReplacesParameterInFront()
    {
        string url = "somepage.aspx?id=SomeId&cat=22&param2=4";
        url = regex.Replace(url, "$1");
        Assert.AreEqual("somepage.aspx?cat=22&param2=4", url);
    }

    [TestMethod]
    public void RegexReplacesParameterAtEnd()
    {
        string url = "somepage.aspx?cat=22&param2=4&id=SomeId";
        url = regex.Replace(url, "$1");
        Assert.AreEqual("somepage.aspx?cat=22&param2=4&", url);
    }

    [TestMethod]
    public void RegexReplacesSoleParameter()
    {
        string url = "somepage.aspx?id=SomeId";
        url = regex.Replace(url, "$1");
        Assert.AreEqual("somepage.aspx?", url);
    }

    public void RegexIgnoresMissingParameter()
    {
        string url = "somepage.aspx?foo=bar&blet=monkey";
        url = regex.Replace(url, "$1");
        Assert.AreEqual("somepage.aspx?foo=bar&blet=monkey", url);
    }

The regex, interpreted, says:

Look for a "?" or an "&" character (and store it as a backreference)
followed by "id="
followed by one or more non-"&" characters.
optionally followed by another "&"

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.

Problem

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&param2=4 ``` or ``` somepage.aspx?cat=tect&id=450 ``` I want to be left with ``` somepage.aspx?cat=22&param2=4 ``` or ``` somepage.aspx?cat=tect ```

Original source

Related problems