Java regex template to remove a parameter from a query string

I am looking for the removal of a parameter fooand its value from all possible query strings in Java.

Is there a regex pattern?

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 rows will be

http://localhost/test?foobar=def 
http://localhost/test?foobar=def
http://localhost/test
http://localhost/test?foobar=def&foobar2=def
+5
source share
3 answers

This regular expression must match the GET parameter and its value ...

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

RegExr .

Just replace it with an empty string.

+18
source

For reference, this other question has a better (Perl) regular expression: Regular expression to remove a single parameter from the 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;
}
+2
source

mchr.

URL- , javascript, . , url , . , URL- "" URL-;-) , .

. foo /test?foo&me=52

, "foo" "test", .

, 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;
  }

}
0

All Articles