【问题标题】:Java regex pattern to remove a parameter from query string用于从查询字符串中删除参数的 Java 正则表达式模式
【发布时间】:2012-02-29 18:29:36
【问题描述】:

我正在寻找从 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

【问题讨论】:

  • 结果字符串应该是什么?

标签: java regex


【解决方案1】:

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

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

RegExr.

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

【讨论】:

  • 太棒了。如果可能的话,你能解释一下它是如何工作的吗?
  • @sandeepnair85 如果你研究每个元字符会更好。有一些关于正则表达式的知识很难放在这个评论框中:)
  • @alex,我认为您的意思是使用后视 - (?&lt;![?&amp;;]) - 而不是前瞻。此外,如果您将该匹配项替换为一个空字符串,则如果有尾随分隔符,您将丢弃它。
  • @AlanMoore 哎呀。删除尾随分隔符是有意的,因为它似乎留下了一个 完整 GET 参数列表。
  • 哦,对了。当然,它必须是积极的向后看,而不是消极的。我现在醒了。 :-/
【解决方案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;
}

【讨论】:

    【解决方案3】:

    这是answer provided by mchr 的扩展。

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

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

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

    【讨论】:

      【解决方案4】:
      url=url.replaceAll("(&"+param+"=[^&]*\$)|(\\?"+param+"=[^&]*\$)|("+param+"=[^&]*&)","")
      

      【讨论】:

      • 稍微描述一下你所做的会更有帮助
      猜你喜欢
      • 2014-05-21
      • 2010-12-22
      • 2015-12-31
      • 1970-01-01
      • 2019-08-06
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多