gpt4 book ai didi

java - 使用正则表达式获取指定查询字符串中 paramName 的 paramValue

转载 作者:行者123 更新时间:2023-12-02 07:34:19 25 4
gpt4 key购买 nike

我喜欢编写一个 java 实用程序方法,该方法返回指定查询字符串中 paramName 的 paramValue

Pattern p = Pattern.compile("\\&?(\\w+)\\=  (I don't know what to put here) ");

public String getParamValue(String entireQueryString, String paramName)
{
Matcher m = p.matcher(entireQueryString);
while(m.find()) {
if(m.group(1).equals(paramName)) {
return m.group(2);
}
}
return null;
}

我将从我的 servlet 中调用此方法,

    String qs = request.getQueryString(); //action=initASDF&requestId=9078-32&redirect=http://www.mydomain.com?actionId=4343
System.out.println(getParamValue(qs, "requestId"));

输出应该是,9078-32

最佳答案

您可以使用正则表达式否定组。请参阅另一个问题:Regular Expressions and negating a whole character group

您需要获取除 & 之外的所有内容。

关于java - 使用正则表达式获取指定查询字符串中 paramName 的 paramValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12475835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com