Java regex模式从查询字符串中删除参数

时间:2012-02-08 10:16:53

标签: java regex

我正在寻找从Java中所有可能的后续查询字符串中删除foo参数及其值。

有没有正则表达式模式吗?

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

结果字符串为

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

4 个答案:

答案 0 :(得分:18)

此正则表达式应与GET参数及其值相匹配...

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

RegExr

只需用空字符串替换它。

答案 1 :(得分:2)

作为参考,在另一个问题中有一个更好的(Perl)正则表达式:Regular expression to remove one parameter from query string

在Java中,这可以通过以下方式实现:

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

答案 2 :(得分:0)

这是answer provided by mchr的扩展。

它允许从URL和查询字符串中删除params,并显示如何执行他提到的javascript测试用例。由于它使用了正则表达式,因此它将返回包含所有其他参数的url。如果您想在“签名”网址时从网址中删除一些参数,这非常有用;-)请注意,这不会删除任何完全空的参数。

e.g。它不会从foo

中删除/test?foo&me=52

下面列出的测试用例会在查询字符串中找到参数“foo”和“test”。

您可以在第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;
  }

}

答案 3 :(得分:0)

url=url.replaceAll("(&"+param+"=[^&]*\$)|(\\?"+param+"=[^&]*\$)|("+param+"=[^&]*&)","")