我有类似这样的传入数据
http://localhost:1111/search?id=10&time=3200&type=abc
http://localhost:1111/search?time=3200&id=11&type=abc
http://localhost:1111/search?id=12
http://localhost:1111/search?id=13&time=3200&type=abc
数据各不相同,但不是完全随机或不可预测的。
那么基本上我们如何提取每个字符串中传入的 ID 并忽略其余的垃圾?
最佳答案
您可以尝试使用正则表达式id=(\d+)
并提取第一个capturing group的值:
String url = "http://localhost:1111/search?id=10&time=3200&type=abc";
Pattern id = Pattern.compile("id=(\\d+)");
Matcher m = id.matcher(url);
if (m.find())
System.out.println(m.group(1));
10
关于用于提取数据的 Java 正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20976930/