In Java I have:
String params = \"depCity=PAR&roomType=D&depCity=NYC\";
I want to get values of depCity
parameters (PA
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.