Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 654503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:30:06+00:00 2026-05-13T22:30:06+00:00

I need to strip out any &id=SomeValue from a Url.PathAndQuery. Where SomeValue could be

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:30:06+00:00Added an answer on May 13, 2026 at 10:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to strip out any movies that are not hosted by YouTube from
I need to strip out all font tags from a document. When attempting to
I need to be able to strip out a condition in a string, here
I need to strip any non-alphanumeric characters from the end of strings using PHP's
I want to strip out <p> and </p> from a string (lets say s
For directory-naming algorithm, need to strip vowels (and white space) from string to shorten
I'm writing a shell script and I need to strip FIND ME out of
I need to strip leading spaces from a column in Oracle. I've Googled but
I need to strip all xml tags from an xml document, but keep the
My need is to strip embedded line breaks but not paragraph breaks when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.