在Java中,我有:
String params = "depCity=PAR&roomType=D&depCity=NYC";
我想要获取depCity
参数(PAR,NYC)的值。
所以我创建了regex:
String regex = "depCity=([^&]+)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(params);
m.find()
返回false。m.groups()
正在返回IllegalArgumentException
。
我做错了什么?
发布于 2011-05-06 01:58:21
它不一定是正则表达式。因为我认为没有标准的方法来处理这件事,所以我使用的是我从某个地方复制的东西(可能还做了一些修改):
public static Map<String, List<String>> getQueryParams(String url) {
try {
Map<String, List<String>> params = new HashMap<String, List<String>>();
String[] urlParts = url.split("\\?");
if (urlParts.length > 1) {
String query = urlParts[1];
for (String param : query.split("&")) {
String[] pair = param.split("=");
String key = URLDecoder.decode(pair[0], "UTF-8");
String value = "";
if (pair.length > 1) {
value = URLDecoder.decode(pair[1], "UTF-8");
}
List<String> values = params.get(key);
if (values == null) {
values = new ArrayList<String>();
params.put(key, values);
}
values.add(value);
}
}
return params;
} catch (UnsupportedEncodingException ex) {
throw new AssertionError(ex);
}
}
所以,当你调用它的时候,你会得到所有的参数和它们的值。该方法处理多值参数,因此是List<String>
而不是String
,在本例中,您需要获取第一个列表元素。
发布于 2011-05-06 03:36:43
不确定您是如何使用find
和group
的,但这可以很好地工作:
String params = "depCity=PAR&roomType=D&depCity=NYC";
try {
Pattern p = Pattern.compile("depCity=([^&]+)");
Matcher m = p.matcher(params);
while (m.find()) {
System.out.println(m.group());
}
} catch (PatternSyntaxException ex) {
// error handling
}
但是,如果您只需要值,而不需要键depCity=
,那么您可以使用m.group(1)
或使用带lookarounds的正则表达式:
Pattern p = Pattern.compile("(?<=depCity=).*?(?=&|$)");
它在与上面相同的Java代码中工作。它试图在depCity=
之后找到一个起始位置。然后匹配尽可能少的任何内容,直到它到达面向&
的点或输入的末尾。
发布于 2014-08-13 23:13:34
我有三个解决方案,第三个是Bozho的改进版本。
首先,如果您不想自己编写东西,而只是使用库,那么可以使用Apache的httpcomponent库的URIBuilder类:http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URIBuilder.html
new URIBuilder("http://...").getQueryParams()...
第二:
// overwrites duplicates
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
public static Map<String, String> readParamsIntoMap(String url, String charset) throws URISyntaxException {
Map<String, String> params = new HashMap<>();
List<NameValuePair> result = URLEncodedUtils.parse(new URI(url), charset);
for (NameValuePair nvp : result) {
params.put(nvp.getName(), nvp.getValue());
}
return params;
}
第三:
public static Map<String, List<String>> getQueryParams(String url) throws UnsupportedEncodingException {
Map<String, List<String>> params = new HashMap<String, List<String>>();
String[] urlParts = url.split("\\?");
if (urlParts.length < 2) {
return params;
}
String query = urlParts[1];
for (String param : query.split("&")) {
String[] pair = param.split("=");
String key = URLDecoder.decode(pair[0], "UTF-8");
String value = "";
if (pair.length > 1) {
value = URLDecoder.decode(pair[1], "UTF-8");
}
// skip ?& and &&
if ("".equals(key) && pair.length == 1) {
continue;
}
List<String> values = params.get(key);
if (values == null) {
values = new ArrayList<String>();
params.put(key, values);
}
values.add(value);
}
return params;
}
https://stackoverflow.com/questions/5902090
复制相似问题