Extract parameters and their values ββfrom a query string in Java
So let's say I have a line like
"param1=value1¶m2={"url":"http://somesite.com?someparam=somevalue&someparam1=somevalue1"}¶m3=value3"
and I need this:
param1: value1
param2: {"url":"http://somesite.com?someparam=somevalue&someparam1=somevalue1"}
param3: value3
What would be the best approach to parsing in Java? Until now, I couldn't find a solution with the Java standard libraries and I don't want to reinvent the wheel.
I've tried (but it doesn't work if I only put query parameters in there like mine):
String url = "http://www.example.com/something.html?one=11111&two=22222&three=33333";
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
for (NameValuePair param : params) {
System.out.println(param.getName() + " : " + param.getValue());
}
source to share
Why don't you use something like a regex:
for example like this one ".*\\?param1=(.*)¶m2=(.*)¶m3=(.*)$"
, it works for your sample url, so I added .*\\?
part;)
and this will work for the first sample ( "param1=value1¶m2={"url":"http://somesite.com?someparam=somevalue&someparam1=somevalue1"}¶m3=value3"
-> param1=(.*)¶m2=(.*)¶m3=(.*)$
Of course, if your parameter names are not something you don't know about
source to share