Reputation: 45
I have two string types:
First : key1=value1&key2=value2&key3=value3...
Second : key1=value
How can I use a Java regular expression to check whether a string is of the first or second type? Example:
String str1 = "a=1&b=2"; // true
String str2 = "a=1&"; // false
String str3 = "a=1"; // true
Upvotes: 0
Views: 732
Reputation: 39355
Assuming the values are only alphanumeric characters.
String text = "a=1&b=2";
boolean match = text.matches("[a-zA-Z0-9]+=[a-zA-Z0-9]+(&[a-zA-Z0-9]+=[a-zA-Z0-9]+)*");
Upvotes: 1