0

Possible Duplicate:
How to extract parameters from a given url

I'm trying to retrieve just the numbers from the parameter in this url:

htt://tesing12/testds/fdsa?communityUuid=45352-32452-52

I have tried this with no luck:

^.*communityUuid=

Any help would be nice.

5
  • Why not just search for communityUuid= and grab everything after that index. I don't see what you want to use a regex. Commented Dec 3, 2012 at 20:27
  • Will the url have more data after that? Commented Dec 3, 2012 at 20:28
  • because i am not guarantied that it is the last item in the string, perhaps a better example is htt://tesing12/testds/fdsa?communityUuid=45352-32452-52?topic=890531-532-532 Commented Dec 3, 2012 at 20:29
  • The example from your comment is not how a URL with multiple parameters would be constructed. Each successive parameter would have a & character in front of it. So... you still don't need a regex. Commented Dec 3, 2012 at 20:32
  • 1
    There are a lot of libraries that can do this for you: stackoverflow.com/questions/218608/… Commented Dec 3, 2012 at 20:34

2 Answers 2

4

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
}

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").

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is better than my answer.
Note that the parameter names and values need to be passed through URLDecoder.decode().
3

I don't see any reason to use a regex.

I would just do this:

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);

NOTE:

You might have to look for the next parameter as well:

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("&.*$", "");

2 Comments

In your sample code above, wouldn't index have the value of index of the "c" in the string communityUuid? The string "theNumbers" would thus be communityUuid=45352-32452-52 which is not what the OP asks for. The int variable needs to be incremented appropriately. Suggest you update your answer as its conceptually correct! I can then upvote it.
@AnuragKapur - Good point... about to edit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.