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.

Community
  • 1
  • 1
user1103205
  • 175
  • 2
  • 13
  • Why not just search for `communityUuid=` and grab everything after that index. I don't see what you want to use a regex. – jahroy Dec 03 '12 at 20:27
  • Will the url have more data after that? – Javier Diaz Dec 03 '12 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 – user1103205 Dec 03 '12 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. – jahroy Dec 03 '12 at 20:32
  • 1
    There are a lot of libraries that can do this for you: http://stackoverflow.com/questions/218608/a-good-library-to-do-url-manipulation-in-java – hoaz Dec 03 '12 at 20:34

2 Answers2

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.

janoside
  • 2,625
  • 1
  • 17
  • 12
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("&.*$", "");
jahroy
  • 22,322
  • 9
  • 59
  • 108
  • 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. – Anurag Kapur Dec 03 '12 at 20:33
  • @AnuragKapur - Good point... about to edit. – jahroy Dec 03 '12 at 20:47