Quan IT
Quan IT

Reputation: 45

Check parameter string Java regex

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

Answers (1)

Sabuj Hassan
Sabuj Hassan

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

Related Questions