简体   繁体   English

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

[英]Java regex pattern to remove a parameter from query string

I am looking for removing foo parameter and its value from all the possible following query strings in Java. 我正在寻找从Java中所有可能的后续查询字符串中删除foo参数及其值。

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

This regex should match the GET param and its value... 这个正则表达式应该与GET参数及其值相匹配......

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

RegExr . RegExr

Just replace it with an empty string. 只需用空字符串替换它。

For reference, there is a better (Perl) regex available in this other question: Regular expression to remove one parameter from query string 作为参考,在另一个问题中有一个更好的(Perl)正则表达式: 正则表达式从查询字符串中删除一个参数

In Java, this can be implemented as follows: 在Java中,这可以实现如下:

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

This is an extension of the answer provided by mchr . 这是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. 它允许从URL和查询字符串中删除params,并显示如何执行他提到的javascript测试用例。 Since it uses a regex, it will return the url with all the other parameters exactly where they were before. 由于它使用了正则表达式,因此它将返回包含所有其他参数的url。 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. 如果您想在“签名”网址时从网址中删除一些参数,这非常有用;-)请注意,这不会删除任何完全空的参数。

eg it will not remove foo from /test?foo&me=52 例如,它不会从/test?foo&me=52删除foo

The test cases listed below remove the parameters, "foo" and "test" when found in the query string. 下面列出的测试用例会在查询字符串中找到参数“foo”和“test”。

You can test it out line here at Repl.it 你可以在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;
  }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM