Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract parameters from a given url

Tags:

In Java I have:

String params = "depCity=PAR&roomType=D&depCity=NYC"; 

I want to get values of depCity parameters (PAR,NYC).

So I created regex:

String regex = "depCity=([^&]+)"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(params); 

m.find() is returning false. m.groups() is returning IllegalArgumentException.

What am I doing wrong?

like image 381
gospodin Avatar asked May 05 '11 17:05

gospodin


People also ask

How can I get parameters from a URL string?

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it.

How do you separate parameters in a URL?

URL parameters are made of a key and a value, separated by an equal sign (=). Multiple parameters are each then separated by an ampersand (&).

What is used to extract the query parameters from the URL?

QueryParam annotation in the method parameter arguments. The following example (from the sparklines sample application) demonstrates using @QueryParam to extract query parameters from the Query component of the request URL.


2 Answers

It doesn't have to be regex. Since I think there's no standard method to handle this thing, I'm using something that I copied from somewhere (and perhaps modified a bit):

public static Map<String, List<String>> getQueryParams(String url) {     try {         Map<String, List<String>> params = new HashMap<String, List<String>>();         String[] urlParts = url.split("\\?");         if (urlParts.length > 1) {             String query = urlParts[1];             for (String param : query.split("&")) {                 String[] pair = param.split("=");                 String key = URLDecoder.decode(pair[0], "UTF-8");                 String value = "";                 if (pair.length > 1) {                     value = URLDecoder.decode(pair[1], "UTF-8");                 }                  List<String> values = params.get(key);                 if (values == null) {                     values = new ArrayList<String>();                     params.put(key, values);                 }                 values.add(value);             }         }          return params;     } catch (UnsupportedEncodingException ex) {         throw new AssertionError(ex);     } } 

So, when you call it, you will get all parameters and their values. The method handles multi-valued params, hence the List<String> rather than String, and in your case you'll need to get the first list element.

like image 191
Bozho Avatar answered Sep 18 '22 13:09

Bozho


Not sure how you used find and group, but this works fine:

String params = "depCity=PAR&roomType=D&depCity=NYC";  try {     Pattern p = Pattern.compile("depCity=([^&]+)");     Matcher m = p.matcher(params);     while (m.find()) {         System.out.println(m.group());     }  } catch (PatternSyntaxException ex) {     // error handling } 

However, If you only want the values, not the key depCity= then you can either use m.group(1) or use a regex with lookarounds:

Pattern p = Pattern.compile("(?<=depCity=).*?(?=&|$)"); 

It works in the same Java code as above. It tries to find a start position right after depCity=. Then matches anything but as little as possible until it reaches a point facing & or end of input.

like image 21
Staffan Nöteberg Avatar answered Sep 18 '22 13:09

Staffan Nöteberg