我正在尝试从此网址中的参数中仅检索数字:
htt://tesing12/testds/fdsa?communityUuid=45352-32452-52
我试过这个没有运气:
^.*communityUuid=
任何帮助都会很好。
答案 0 :(得分:4)
我建议不要使用简单的字符串操作路径。它更冗长,更容易出错。您也可以从内置类中获得一些帮助,然后使用您使用URL(用“&”分隔的参数)的知识来指导您的实现:
String queryString = new URL("http://tesing12/testds/fdsa?communityUuid=45352-32452-52").getQuery();
String[] params = queryString.split("&");
String communityUuid = null;
for (String param : params) {
if (param.startsWith("communityUuid=")) {
communityUuid = param.substring(param.indexOf('=') + 1);
}
}
if (communityUuid != null) {
// do what you gotta do
}
这为您提供了检查URL格式良好的好处,并避免了类似命名参数可能产生的问题(字符串操作路由将报告“abc_communityUuid”以及“communityUuid”的值)
此代码的有用扩展是在迭代“params”时构建映射,然后在地图中查询所需的任何参数名称。
答案 1 :(得分:3)
我认为没有理由使用正则表达式。
我会这样做:
String token = "communityUuid=";
String url = "htt://tesing12/testds/fdsa?communityUuid=45352-32452-52";
int index = url.indexOf(token) + token.length();
String theNumbers = url.substring(index);
注:
您可能还需要查找下一个参数:
String token = "communityUuid=";
String url = "htt://tesing12/testds/fdsa?communityUuid=45352-32452-52";
int startIndex = url.indexOf(token) + token.length();
// here's where you might want to use a regex
String theNumbers = url.substring(startIndex).replaceAll("&.*$", "");