regex to match non-whitespace around & in Java

I have a request query string which contains parameter name with & (&name) since in request query string, parameters also separated with &, then variable name is &&name. I wrote the regexp which is should return parameter value

Matcher matcher = Pattern.compile("&(&[^=]+)=([^&]*)").matcher(str);
while (matcher.find()) {
String value = matcher.group(2);
    System.out.println(value);
}

So regexp works fine when request string has following construction:

  String str = "&&NAME=name 1&&NAME=name 2&type=1";

output is : name 1
name 2

But when the string has the following construction

String str = "&&NAME=name 1&&NAME=name & 2&type=1";

output is : name 1
name

I tried this &(&[^=]+)=([^S&S]*) – S Matches non-whitespace, but nor worked.
How can I solve it?


Source: regex

Leave a Reply