如何从给定的URL中提取参数
在Java中我有:
String params = "depCity=PAR&roomType=D&depCity=NYC";
我想获得depCity
参数的值(PAR,NYC)。
所以我创建了正则表达式:
String regex = "depCity=([^&]+)"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(params);
m.find()
返回false。 m.groups()
返回IllegalArgumentException
。
我究竟做错了什么?
它不一定是正则表达式。 因为我认为没有标准的方法来处理这个问题,我使用的是我从某个地方复制过的东西(也许有点修改过):
public static Map> getQueryParams(String url) { try { Map> params = new HashMap>(); 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 values = params.get(key); if (values == null) { values = new ArrayList (); params.put(key, values); } values.add(value); } } return params; } catch (UnsupportedEncodingException ex) { throw new AssertionError(ex); } }
因此,当您调用它时,您将获得所有参数及其值。 该方法处理多值params,因此List
而不是String
,在您的情况下,您将需要获取第一个list元素。
不确定你是如何使用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)
或使用带有外观的正则表达式:
Pattern p = Pattern.compile("(?<=depCity=).*?(?=&|$)");
它使用与上面相同的Java代码。 它试图在depCity=
之后找到一个起始位置。 然后匹配任何东西,但尽可能少,直到它到达面向&
或输入结束的点。
如果您正在开发Android应用程序,请尝试以下方法:
String yourParam = null; Uri uri = Uri.parse(url); try { yourParam = URLDecoder.decode(uri.getQueryParameter(PARAM_NAME), "UTF-8"); } catch (UnsupportedEncodingException exception) { exception.printStackTrace(); }
我有三个解决方案,第三个是Bozho的改进版本。
首先,如果你不想自己编写内容并只是使用lib,那么使用Apache的httpcomponents lib的URIBuilder类: http ://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/ HTTP /客户/ utils的/ URIBuilder.html
new URIBuilder("http://...").getQueryParams()...
第二:
// overwrites duplicates import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; public static Map readParamsIntoMap(String url, String charset) throws URISyntaxException { Map params = new HashMap<>(); List result = URLEncodedUtils.parse(new URI(url), charset); for (NameValuePair nvp : result) { params.put(nvp.getName(), nvp.getValue()); } return params; }
第二:
public static Map> getQueryParams(String url) throws UnsupportedEncodingException { Map> params = new HashMap>(); 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 values = params.get(key); if (values == null) { values = new ArrayList (); params.put(key, values); } values.add(value); } return params; }
Simple Solution从所有param名称和值中创建地图并使用它:)。
import org.apache.commons.lang3.StringUtils; public String splitURL(String url, String parameter){ HashMap urlMap=new HashMap(); String queryString=StringUtils.substringAfter(url,"?"); for(String param : queryString.split("&")){ urlMap.put(StringUtils.substringBefore(param, "="),StringUtils.substringAfter(param, "=")); } return urlMap.get(parameter); }
如果类路径中存在spring-web , 则可以使用UriComponentsBuilder 。
MultiValueMap queryParams = UriComponentsBuilder.fromUriString(url).build().getQueryParams();