String url = "mysite.com/index.php?name=john&id=432"
如何提取 ID 参数 (432)?
它可以在URL的任何位置上, ID的长度也不同
String url = "mysite.com/index.php?name=john&id=432"
如何提取 ID 参数 (432)?
它可以在URL的任何位置上, ID的长度也不同
http://hc.apache.org/http://hc.ahrf="http://hc.apache.org/httpsublicents-client-ga/httpclient/apidocs/org/pache/http/client/utils/URLEncoddUtils.html" rel=“nreferrer”>URLEncodUtils HtpClient 包:
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import java.nio.charset.Charset;
import java.util.List;
public class UrlParsing {
public static void main(String[] a){
String url="http://mysite.com/index.php?name=john&id=42";
List<NameValuePair> args= URLEncodedUtils.parse(url, Charset.defaultCharset());
for (NameValuePair arg:args)
if (arg.getName().equals("id"))
System.out.println(arg.getValue());
}
}
此打印 42 到控制台 。
If you have the url stored in a URI object, you may find useful an overload of URLEncodedUtils.parse that accept directly an URI instance. If you use this overloaded version, you have to give the charset as a string:
URI uri = URI.create("http://mysite.com/index.php?name=john&id=42");
List<NameValuePair> args= URLEncodedUtils.parse(uri, "UTF-8");
我只是给一个抽象的regex。 在 之后添加您不希望在
id
中出现的任何内容 [\\ amp;
Pattern pattern = Pattern.compile("id=([^&]*?)$|id=([^&]*?)&");
Matcher matcher = pattern.matcher(url);
if (matcher.matches()) {
int idg1 = Integer.parseInt(matcher.group(1));
int idg2 = Integer.parseInt(matcher.group(2));
}
idg1
或 idg2
都有价值。
您可以使用:
String id = url.replaceAll("^.*?(?:\?|&)id=(\d+)(?:&|$).*$", "$1");
regex已经给出了, 但你也可以通过一些简单的分裂来做到这一点:
public static String getId(String url) {
String[] params = url.split("\?");
if(params.length==2) {
String[] keyValuePairs = params[1].split("&");
for(String kvp : keyValuePairs) {
String[] kv = kvp.split("=");
if(kv[0].equals("id")) {
return kv[1];
}
}
}
throw new IllegalStateException("id not found");
}
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...