可能重复:
How to extract parameters from a given urljava的正则表达式URL解析
我想从这个URL参数只检索的数字:
htt://tesing12/testds/fdsa?communityUuid=45352-32452-52
我已经试过这个没有运气:
^.*communityUuid=
任何帮助将是很好的。
可能重复:
How to extract parameters from a given urljava的正则表达式URL解析
我想从这个URL参数只检索的数字:
htt://tesing12/testds/fdsa?communityUuid=45352-32452-52
我已经试过这个没有运气:
^.*communityUuid=
任何帮助将是很好的。
我建议不要简单的字符串操作路线。它更冗长,更容易出错。你可能也得到了内置类一点点帮助,然后用你的,你用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”时构建地图,然后查询地图以获取所需的任何参数名称。
我看不出有任何理由使用正则表达式。
我只是这样做:
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("&.*$", "");
在上面的示例代码中,不会索引字符串communityUuid中的“c”的索引值吗?字符串“theNumbers”因此将是communityUuid = 45352-32452-52,这不是OP所要求的。 int变量需要适当增加。建议你更新你的答案,因为它的概念是正确的!然后,我可以放弃它。 –
太棒了,感谢您的帮助=)。 – user1103205
@AuuragKapur - 好点...即将编辑。 – jahroy
为什么不直接搜索'communityUuid ='并在该索引后面抓取所有内容。我没有看到你想要使用正则表达式。 – jahroy
之后的网址会有更多的数据吗? –
,因为我不担保它是字符串中的最后一项,或许更好的示例是htt:// tesing12/testds/fdsa?communityUuid = 45352-32452-52?topic = 890531-532-532 – user1103205