1
String url = "mysite.com/index.php?name=john&id=432"

How can I extract the id parameter (432)?

it can be in any position in the url and the length of the id varies too

5
  • Using a regex is probably the wrong way to go about this - is there a really good reason for using one? Commented May 23, 2012 at 21:30
  • @PhilipKendall no particular reason, i want want to retrieve the value of id Commented May 23, 2012 at 21:31
  • 2
    I understand this approach in script languages or in .htaccess but in java we can use the URL class to parte urls and it is easy to test and maintain. Keep it in mind: you can use regex but probably there is a better way to solve this problem (maybe using regex, maybe not). Commented May 23, 2012 at 21:32
  • @TiagoPeczenyj how? can you post an example Commented May 23, 2012 at 21:35
  • see this thread : stackoverflow.com/questions/1667278/… Commented May 23, 2012 at 21:56

4 Answers 4

6

You can use Apache URLEncodedUtils from HttpClient package:

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import java.nio.charset.Charset;
import java.util.List;

public class UrlParsing {
    public static void main(String[] a){
        String url="http://mysite.com/index.php?name=john&id=42";
        List<NameValuePair> args= URLEncodedUtils.parse(url, Charset.defaultCharset());
        for (NameValuePair arg:args)
            if (arg.getName().equals("id"))
                System.out.println(arg.getValue());
    }
}

This print 42 to the console.

If you have the url stored in a URI object, you may find useful an overload of URLEncodedUtils.parse that accept directly an URI instance. If you use this overloaded version, you have to give the charset as a string:

URI uri = URI.create("http://mysite.com/index.php?name=john&id=42");
List<NameValuePair> args= URLEncodedUtils.parse(uri, "UTF-8");
Sign up to request clarification or add additional context in comments.

3 Comments

it complains about Charset.defaultCharset() it needs a string instead. whats the encoding string look like?
It's working for me. I'm using version 4.2 of httpclient. Beware that my first version was wrong: I edited my code: are you using the code before my edit?
Looked at URLEncodedUtils javadoc: it wnat a String as a second parameter if you give the url as an URI instance; otherwise, it accept a Charset as in my code
3

I just give an abstract regex. add anything you don't want in id after [^&

Pattern pattern = Pattern.compile("id=([^&]*?)$|id=([^&]*?)&");

Matcher matcher = pattern.matcher(url);

if (matcher.matches()) {
    int idg1   = Integer.parseInt(matcher.group(1));
    int idg2   = Integer.parseInt(matcher.group(2));
}

either idg1 or idg2 has value.

Comments

0

You can use:

String id = url.replaceAll("^.*?(?:\\?|&)id=(\\d+)(?:&|$).*$", "$1");

5 Comments

fails if it's the first param :)
that returns the id and the rest of the url (the other parameters). output 432&pId=10....
@user521180: So sorry, omitted the .*$ part earlier, pls check the edited answer now.
@user521180: Are you sure, what is your input URL? For me it always returns parameter id's value.
wow - and people have trouble reading regex and figuring out what they do :-)
0

The regex has already been given, but you can do it with some simple splitting too:

public static String getId(String url) {
        String[] params = url.split("\\?");
        if(params.length==2) {
                String[] keyValuePairs = params[1].split("&");
                for(String kvp : keyValuePairs) {
                        String[] kv = kvp.split("=");
                        if(kv[0].equals("id")) {
                                return kv[1];
                        }
                }
        }
        throw new IllegalStateException("id not found");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.