【发布时间】:2020-05-26 21:34:52
【问题描述】:
我从 HTTP 响应标头获取下面显示的 URL(http://localhost:8080/CompanyServices/api/creators/2173),我想在 creators 之后获取 id,即 2173。
所以,我删除了所有非数字,如下所示,得到以下结果:80802173。
从上面的一组数字中获取最后 4 位数字是一个好方法吗?
有一件事是,这部分localhost:8080 可能会根据我部署我的应用程序的服务器而改变,所以我想知道我是否应该在creators/ 之后抓住一些东西?如果是,那么最好的方法是什么?
public class GetLastFourIDs {
public static void main(String args[]){
String str = "http://localhost:8080/CompanyServices/api/creators/2173";
String replaceString=str.replaceAll("\\D+","");
System.out.println(replaceString);
}
}
【问题讨论】:
-
使用:
(?<=creators/)\d+ -
使用
String id = str.replaceFirst("^.*?(\\d+)$", "$1"); -
@Andreas 你能解释一下
String id = str.replaceFirst("^.*?(\\d+)$", "$1");吗?这行得通。