从查询字符串中删除参数的Java正则表达式模式
我希望从Java中所有可能的查询字符串中删除从查询字符串中删除参数的Java正则表达式模式,java,regex,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://lo
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
此正则表达式应与GET参数及其值匹配
(?<=[?&;])foo=.*?($|[&;])
(?作为参考,在另一个问题中有一个更好的(Perl)正则表达式:
在Java中,这可以按如下方式实现:
public static String removeParams(String queryString, String... params) {
for (String param : params) {
String keyValue = param + "=[^&]*?";
queryString = queryString.replaceAll("(&" + keyValue + "(?=(&|$))|^" + keyValue + "(&|$))", "");
}
return queryString;
}
这是一个扩展的
它允许从url和查询字符串中删除参数,并显示如何执行他提到的javascript测试用例。由于它使用正则表达式,它将返回url以及之前的所有其他参数。如果在“签名”url时要从url中删除某些参数,这非常有用;-)请注意,这不会删除任何完全为空的参数
e、 g.它不会从/test?foo&me=52
下面列出的测试用例删除了查询字符串中的参数“foo”和“test”
你可以在线测试它
结果应该是什么?太棒了。如果可能的话,你能解释一下它是如何工作的吗?@sandepnair85如果你研究每个元字符会更好。关于regex有一点需要了解,但很难放在这个评论框中:)@alex,我想你是想使用lookback-(?-不是向前看。而且,如果用空字符串替换该匹配项,如果有尾随分隔符,则会将其丢弃。@Alanmore Oops。删除尾随分隔符是有意的,因为它似乎会留下完整的GET参数列表。哦,对了。当然,它必须是正向向后看,而不是反向。我很抱歉现在开始。:-/对你所做的做一点描述会更有帮助
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+"=[^&]*&)","")