Java正则表达式模式:从查询字符串中删除参数

6

我希望能够通过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

那么应该得到什么样的字符串呢? - anubhava
4个回答

21

这个正则表达式应该匹配GET参数及其值...

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

RegExr

只需将其替换为空字符串。


太棒了。如果可能的话,你能解释一下它是如何完成工作的吗? - Sandeep Nair
@sandeepnair85 如果你能研究一下每个元字符,那会更好。关于正则表达式有很多需要了解的内容,这些内容很难在评论框中涵盖 :) - alex
1
@alex,我认为你应该使用lookbehind - (?<![?&;]) - 而不是 lookahead。此外,如果您用空字符串替换该匹配项,则会丢弃尾随分隔符(如果有的话)。 - Alan Moore
@AlanMoore 抱歉,删除尾随分隔符是有意的,因为它似乎留下了一个完整的 GET 参数列表。 - alex
哦,没错。当然,它必须是正向回顾,而不是负向的。我现在清醒了。 :-/ - Alan Moore
显示剩余2条评论

2

供参考,这里有一个更好的(Perl)正则表达式可用于另一个问题:从查询字符串中删除一个参数的正则表达式

在Java中,可以按照以下方式实现:

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

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

2
简要说明您所做的工作将更有帮助。 - Basil Battikhi

0

这是mchr提供的答案的扩展。

它允许从URL和查询字符串中删除参数,并显示如何执行他所提到的javascript测试用例。由于它使用正则表达式,因此将返回具有所有其他参数的URL,其位置与之前完全相同。如果您想要在“签名”URL时从URL中删除某些参数,则此方法非常有用;-)请注意,这不会删除任何完全为空的参数。

例如,它不会从/test?foo&me=52中删除foo

下面列出的测试用例会在查询字符串中找到“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;
  }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接