Java正则表达式URL解析 [英] java regular expression url parsing
问题描述
可能重复:
如何从给定的URL中提取参数
Possible Duplicate:
How to extract parameters from a given url
我正在尝试仅从此url中的参数中检索数字:
I'm trying to retrieve just the numbers from the parameter in this url:
htt://tesing12/testds/fdsa?communityUuid=45352-32452-52
我没有运气尝试过这个
^.*communityUuid=
任何帮助都会很好.
推荐答案
我建议不要使用简单的字符串操作路线.它更冗长,更容易出错.您还可以从内置类中获得一些帮助,然后利用所使用的URL知识(用&"分隔的参数)来指导您的实现:
I recommend against the simple string-manipulation route. It's more verbose and more error prone. You may as well get a little help from the built-in classes and then use your knowledge that you're working with a URL (parameters delimited with "&") to guide your implementation:
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"的值)
This gives you the benefit of checking the well-formed-ness of the URL and avoids problems that can arise from similarly named parameters (the string-manipulation route will report the value of "abc_communityUuid" as well as "communityUuid").
此代码的一个有用扩展是在您遍历"params"时构建一个映射,然后在映射中查询所需的任何参数名称.
A useful extension of this code is to build a map as you iterate over "params" and then query the map for any parameter name you want.
这篇关于Java正则表达式URL解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!