Using this definition of a URI
<scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ]
I want to split this into three groups; 1) scheme + heirarchy, 2) query, 3) fragment.
Some examples:
http://foo.com/bar?k1=v1&k2=v2#fragment = [http://foo.com/bar, ?k1=v1&k2=v2, #fragment]
http://foo.com/bar?k1=v1&k2=v2 = [http://foo.com/bar, ?k1=v1&k2=v2, ]
http://foo.com/bar#fragment = [http://foo.com/bar, , #fragment]
http://foo.com/bar = [http://foo.com/bar, , ]
At the moment I have .+(\?[^#]+)(.*)?$ which handles case 1 and 2, 4 is no match which is okay but I’m having trouble trying to edit this to work for case 3.
It can be assumed that the URI is well formed, we care only about the presence of ? and #.
The context is I have a URI input that may have a query and/or fragment part and I need to add additional query parameters to it. So evaluating the presence of each group I can figure out how my parameters should be inserted/appended to the URI.
Implementation is in Java.
Thanks in advance.
Try this: