6

I am looking for removing foo parameter and its value from all the possible following query strings in Java.

Is there a regex pattern to do this?

http://localhost/test?foo=abc&foobar=def 
http://localhost/test?foobar=def&foo=abc
http://localhost/test?foo=abc
http://localhost/test?foobar=def&foo=abc&foobar2=def

The resulting strings would be

http://localhost/test?foobar=def 
http://localhost/test?foobar=def
http://localhost/test
http://localhost/test?foobar=def&foobar2=def
alex
  • 479,566
  • 201
  • 878
  • 984
Sandeep Nair
  • 3,630
  • 3
  • 26
  • 38

4 Answers4

21

This regex should match the GET param and its value...

(?<=[?&;])foo=.*?($|[&;])

RegExr.

Just replace it with an empty string.

alex
  • 479,566
  • 201
  • 878
  • 984
  • Awesome. Can you please explain if possible how it did it's job? – Sandeep Nair Feb 08 '12 at 10:29
  • @sandeepnair85 It would be better if you researched each meta character. There is a bit to know about regex that is hard to fit in this comment box :) – alex Feb 08 '12 at 10:32
  • 1
    @alex, I think you meant to use a lookbehind - `(?<![?&;])` - not a lookahead. Also, if you replace that match with an empty string, you'll throw away the trailing delimiter if there is one. – Alan Moore Feb 08 '12 at 10:47
  • @AlanMoore Oops. The removal of the trailing delimiter was intentional, as it seemed to leave behind an *intact* list of GET params. – alex Feb 08 '12 at 10:51
  • Oh, right. And of course, it has to be a *positive* lookbehind, not negative. I'm awake now. :-/ – Alan Moore Feb 08 '12 at 11:10
  • @alex Sorry, but this regex doesn't seem to remove '?' for "http://localhost/test?foo=abc" case. – Ilya Buziuk Jan 06 '15 at 16:37
  • @IlyaBuziuk It won't, because it uses a positive lookbehind. If you want to kill the `?` there, then remove the lookbehind and simply consume it with your match. – alex Jan 06 '15 at 23:17
2

For reference, there is a better (Perl) regex available in this other question: Regular expression to remove one parameter from query string

In Java, this can be implemented as follows:

public static String removeParams(String queryString, String... params) {
    for (String param : params) {
        String keyValue = param + "=[^&]*?";
        queryString = queryString.replaceAll("(&" + keyValue + "(?=(&|$))|^" + keyValue + "(&|$))", "");
    }
    return queryString;
}
Community
  • 1
  • 1
mchr
  • 6,161
  • 6
  • 52
  • 74
0

This is an extension of the answer provided by mchr.

It allows params to be removed from an url and from a query string, and shows how to execute the javascript test cases he mentions. Since it uses a regex, it will return the url with all the other parameters exactly where they were before. This is useful if you want to remove some parameters from an url when "signing" an url ;-) Note that this does not remove any completely empty parameters.

e.g. it will not remove foo from /test?foo&me=52

The test cases listed below remove the parameters, "foo" and "test" when found in the query string.

You can test it out line here at Repl.it

class Main {
  public static void main(String[] args) {
    runTests();
  }

  public static void runTests() {
    test("foo=%2F{}/me/you&me=52", "me=52");
    test("?foo=%2F{}/me/you&me=52", "?me=52");
    test("?foo=52&me=able was i ere&test=2", "?me=able was i ere");
    test("foo=",  "");
    test("?",  "");
    test("?foo=52",  "");
    test("test?", "test");
    test("test?foo=23", "test");
    test("foo=&bar=456", "bar=456");
    test("bar=456&foo=", "bar=456");
    test("abc=789&foo=&bar=456", "abc=789&bar=456");
    test("foo=123",  "");
    test("foo=123&bar=456", "bar=456");
    test("bar=456&foo=123", "bar=456");
    test("abc=789&foo=123&bar=456", "abc=789&bar=456");
    test("xfoo", "xfoo");
    test("xfoo&bar=456", "xfoo&bar=456");
    test("bar=456&xfoo", "bar=456&xfoo");
    test("abc=789&xfoo&bar=456", "abc=789&xfoo&bar=456");
    test("xfoo=", "xfoo=");
    test("xfoo=&bar=456", "xfoo=&bar=456");
    test("bar=456&xfoo=", "bar=456&xfoo=");
    test("abc=789&xfoo=&bar=456", "abc=789&xfoo=&bar=456");
    test("xfoo=123", "xfoo=123");
    test("xfoo=123&bar=456", "xfoo=123&bar=456");
    test("bar=456&xfoo=123", "bar=456&xfoo=123");
    test("abc=789&xfoo=123&bar=456", "abc=789&xfoo=123&bar=456");
    test("foox", "foox");
    test("foox&bar=456", "foox&bar=456");
    test("bar=456&foox", "bar=456&foox");
    test("abc=789&foox&bar=456", "abc=789&foox&bar=456");
    test("foox=", "foox=");
    test("foox=&bar=456", "foox=&bar=456");
    test("bar=456&foox=", "bar=456&foox=");
    test("abc=789&foox=&bar=456", "abc=789&foox=&bar=456");
    test("foox=123", "foox=123");
    test("foox=123&bar=456", "foox=123&bar=456");
    test("bar=456&foox=123", "bar=456&foox=123");
    test("abc=789&foox=123&bar=456", "abc=789&foox=123&bar=456");
  }  

  public static void test (String input, String expected) {
    String result = removeParamsFromUrl(input, "foo", "test");
    if (! result.equals(expected))
      throw new RuntimeException("Failed:" + input);
    System.out.println("Passed:" + input + ", output:" + result);
  }


  public static String removeParamsFromQueryString(String queryString, String... params) {
    for (String param : params) {
      String keyValue = param + "=[^&]*?";
      queryString = queryString.replaceAll("(&" + keyValue + "(?=(&|$))|^" + keyValue + "(&|$))", "");
    }

    return queryString;
  }

  public static String removeParamsFromUrl(String url, String... params) {
    String queryString;
    String baseUrl;

    int index = url.indexOf("?");
    boolean wasFullUrl = (index != -1);

    if (wasFullUrl)
    {
      baseUrl = url.substring(0, index);
      queryString = url.substring(index+1);
    }
    else
    {
      baseUrl = "";
      queryString = url;
    }

    String newQueryString = removeParamsFromQueryString(queryString, params);

    String result;
    if (wasFullUrl)
    {
      boolean isEmpty = newQueryString == null || newQueryString.equals("");
      result = isEmpty ? baseUrl : baseUrl + "?" + newQueryString;
    }
    else
    {
      result = newQueryString;
    }

    return result;
  }

}
Community
  • 1
  • 1
Brad Parks
  • 66,836
  • 64
  • 257
  • 336
0
url=url.replaceAll("(&"+param+"=[^&]*\$)|(\\?"+param+"=[^&]*\$)|("+param+"=[^&]*&)","")
Raph
  • 1
  • 1