I want to remove some querystring attribute from my url, I tried some regex but didn't get the exact solution for this. Please help me to out of it. eg:

String qString = "category=Popular&code=14290115";
qString = qString .replaceAll("(?<=[?&;])code=.*?($|[&;])",""); 
output: category=Popular&

the above regex is removing the attribute but not removing & symbol. So please suggest me for this.

有帮助吗?

解决方案

Don't use a regex for this. It's an utter nightmare because any character could be percent-encoded, e.g. ?foo=bar is exactly the same as ?%66oo=%62ar. Parse the URL, then the query string, then rebuild it. Take a look at URIBuilder and URLEncodedUtils out of the Apache Commons HTTP Client.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top