Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top