Reputation: 35
I'm trying to parse (Java) a custom GET style request, and I'd like to do that through a regular expression.
The request is formatted like:
GET myCommand?parameter1=value¶meter2=value¶meter3=value¶meter4=value
The number of params is variable, but at least one param is required.
Can someone help me with this regex?
Upvotes: 1
Views: 3532
Reputation: 388
Following is regex that will work with any request URL with query parameters: .+?(\?((\w+?=.+?)&)*\w+?=.+)?$
Here .+?
is matching string before query parameters. Let break regex: (\?((\w+?=.+?)&)*\w+?=.+)?
((\w+?=.+?)&)*
represents zero or more key-value pairs joined with &
\w+?=.+
represents last key-value pair
Hope this helps. Let me know if you need more help.
Upvotes: 0
Reputation: 425208
Here's how to parse it all into java variables using 4 lines:
String command = input.replaceAll("(^\\w+ )|(\\?.*)", "");
Map<String, String> params = new LinkedHashMap<String, String>();
for (String pair : input.replaceFirst(".*?\\?", "").split("&"))
params.put(pair.split("=")[0], pair.split("=")[1]);
Note that using a LinkedHashMap
iterates in input order.
Here's a little test using your input (modified a little to have distinct values):
public static void main(String[] args) throws Exception {
String input = "GET myCommand?parameter1=value1¶meter2=value2¶meter3=value2¶meter4=value4";
String command = input.replaceAll("(^\\w+ )|(\\?.*)", "");
Map<String, String> params = new LinkedHashMap<String, String>();
for (String pair : input.replaceFirst(".*?\\?", "").split("&"))
params.put(pair.split("=")[0], pair.split("=")[1]);
System.out.println("Command=" + command);
System.out.println("Params=" + params);
}
Output:
Command=myCommand
Params={parameter1=value1, parameter2=value2, parameter3=value2, parameter4=value4}
Upvotes: 3
Reputation: 3155
GET myCommand\?([a-z0-9]+)=(.+)(&([a-z0-9]+)=(.+))*
Now just replace [a-z0-9]+
with a regex for valid identifiers, .+
with a regex for valid values, and escape as required by your language of choice, and you should be good to go.
Upvotes: 0