-1

I have this url

http://host.com/routingRequest?returnJSON=true&timeout=60000&to=s%3A73746647+d%3Afalse+f%3A-1.0+x%3A-74.454383+y%3A40.843021+r%3A-1.0+cd%3A-1.0+fn%3A-1+tn%3A-1+bd%3Atrue+st%3ACampus%7EDr&returnGeometries=true&nPaths=1&returnClientIds=true&returnInstructions=true
&hour=12+00&from=s%3A-1+d%3Afalse+f%3A-1.0+x%3A-74.241765+y%3A40.830182+r%3A-1.0+cd%3A-1.0+fn%3A56481485+tn%3A26459042+bd%3Afalse+st%3AClaremont%7EAve&sameResultType=true&type=HISTORIC_TIME

and i try to fetch

to = -74.454383, 40.843021
from = -74.241765, 40.830182
hour = 12+00

with this code:

    String patternString = "(x%3A) (.+?) (y%3A) (.+?) (r%3A)";

    Pattern pattern = Pattern.compile(patternString);
    Matcher matcher = pattern.matcher(freshResponse.regression_requestUrl);

    H4 h4 = new H4().appendText("Response ID: " + id);
    Ul ul = new Ul();
    Li li1 = new Li();
    Li li2 = new Li();
    if (matcher.find()) {
        li1.appendText("From: " + matcher.group(1) + ", " + matcher.group(2));
    }
    if (matcher.find()) {
        li2.appendText("To: " + matcher.group(1) + ", " + matcher.group(2));
    }

    patternString = "(&hour=) (.+?) (&from=)";
    pattern = Pattern.compile(patternString);
    matcher = pattern.matcher(freshResponse.regression_requestUrl);

    Li li3 = new Li();
    if (matcher.find()) {
        li3.appendText("At: " + matcher.group(1));
    }

but i get no matches. what am i missing?

could I have done this without regex more easily?

2
  • 2
    You should use URI instead, and .getQuery(); it will be much easier to extract what you want afterwards Commented Jul 13, 2015 at 7:59
  • Don't do it. "Parsing" raw URLs with regexes is going to give you horrible problems with edge cases, etc. Commented Jul 13, 2015 at 8:02

1 Answer 1

1
Map params = new HashMap();
String url = "http://host.com/routingRequest?returnJSON=true&timeout=60000&to=s%3A73746647+d%3Afalse+f%3A-1.0+x%3A-74.454383+y%3A40.843021+r%3A-1.0+cd%3A-1.0+fn%3A-1+tn%3A-1+bd%3Atrue+st%3ACampus%7EDr&returnGeometries=true&nPaths=1&returnClientIds=true&returnInstructions=true
&hour=12+00&from=s%3A-1+d%3Afalse+f%3A-1.0+x%3A-74.241765+y%3A40.830182+r%3A-1.0+cd%3A-1.0+fn%3A56481485+tn%3A26459042+bd%3Afalse+st%3AClaremont%7EAve&sameResultType=true&type=HISTORIC_TIME";
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");

for (NameValuePair param : params) {
  map.put(param.getName(),param.getValue());
}

You need to use apache httpclient to get the NameValuePair class.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.