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

124697
  • 22,097
  • 68
  • 188
  • 315
  • Using a regex is probably the wrong way to go about this - is there a really good reason for using one? – Philip Kendall May 23 '12 at 21:30
  • @PhilipKendall no particular reason, i want want to retrieve the value of `id` – 124697 May 23 '12 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). – Tiago Peczenyj May 23 '12 at 21:32
  • @TiagoPeczenyj how? can you post an example – 124697 May 23 '12 at 21:35
  • see this thread : http://stackoverflow.com/questions/1667278/parsing-query-strings-in-java – Tiago Peczenyj May 23 '12 at 21:56

4 Answers4

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");
Andrea Parodi
  • 5,534
  • 27
  • 46
  • it complains about Charset.defaultCharset() it needs a string instead. whats the encoding string look like? – 124697 May 23 '12 at 22:19
  • 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? – Andrea Parodi May 23 '12 at 22:37
  • 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 – Andrea Parodi May 23 '12 at 22:40
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.

Darshana
  • 2,462
  • 6
  • 28
  • 54
0

You can use:

String id = url.replaceAll("^.*?(?:\\?|&)id=(\\d+)(?:&|$).*$", "$1");
anubhava
  • 761,203
  • 64
  • 569
  • 643
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");
}
Matthijs Bierman
  • 1,720
  • 9
  • 14