Paul
Paul

Reputation: 1159

.NET Regular expression for querystring value

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

Upvotes: 2

Views: 5478

Answers (3)

roufamatic
roufamatic

Reputation: 18485

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.

Upvotes: 7

Michael D. Irizarry
Michael D. Irizarry

Reputation: 6302

I would first parse the Querystring to Strongly typed values, then I would check using Regex if I needed to.

C# ASP.NET QueryString parser

Upvotes: 0

Jason Kresowaty
Jason Kresowaty

Reputation: 16500

If bad things could happen (e.g., security-wise) if an "id=" parameter were missed by the regular expression, then you also need to worry that the query string might contain a hexadecimal urlencoded equivalent, which the regular expression will not recognize. For example, "id" is "%69%64". Also consider the effects of different capitalizations of "id" on your program. My opinion in this situtation is that you read the RFCs and build a complete class that can do transformations in both directions from a set of name-value pairs to a query strings. System.Uri will not do this. if you are running inside an ASP.NET application, you might investigate if HttpUtility.ParseQueryString is sufficient.

Upvotes: 0

Related Questions