lets say I have a url param like token=1234235asdjaklj231k209a&name=sam&firname=Mahan
how can I replace the value of the token with new one ?
I've done something similar to this with pattern and matcher before but I don't recall now
but I know there is a way to do so
Update : the token can contain any letter but &
thanks in advance

- 147
- 1
- 3
- 14
-
what the output should look like? – Youcef LAIDANI Sep 19 '17 at 14:29
-
@YCF_L the same string but the value of the token is replaced by the new one like token=1234&name=sam&firname=Mahan that 1234 is the new token for instance and the token contains anything but & – Mahan Sep 19 '17 at 14:30
-
I don't think this is a job for regex; there are well tested abstractions for urls in java. – Andreas Sep 19 '17 at 14:39
-
please explain all the cases you can have so we can help you: – Youcef LAIDANI Sep 19 '17 at 14:41
-
possible duplicate of https://stackoverflow.com/questions/13592236/parse-a-uri-string-into-name-value-collection – Andreas Sep 19 '17 at 14:53
3 Answers
Spring has a util that handles this need gracefully. Apache httpcomponents does too. Below is a spring example.
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
public class StackOverflow {
private static class SO46303058 {
public static void main(String[] args) {
final String urlString = "https://subdomain.hostname/path/resource?token=1234235asdjaklj231k209a&name=sam&firname=Mahan";
final URI uri = UriComponentsBuilder.fromHttpUrl(urlString)
.replaceQueryParam("token", "abc")
.build().toUri();
System.out.println(uri);
}
}
}
Don't be afraid of adding dependencies to your project, it beats reinventing the wheel.

- 4,937
- 2
- 25
- 35
-
1I'm upvoting this because I feel that in general most URI/URL stuff should be handled by such libraries and not manually. – Tim Biegeleisen Sep 19 '17 at 14:53
-
Note to my downvoter: That being said, if the OP for some reason really only had a fragment of a query string, then such a canned library would not work. – Tim Biegeleisen Sep 19 '17 at 15:04
-
1thanks that's great news that I'm using Spring too that this helps a lot and its so clean – Mahan Sep 19 '17 at 15:05
We can consider doing a simple regex replacement, with a few caveats (q.v. below the code snippet).
String url = "token=1234235asdjaklj231k209a&name=sam&firname=Mahan";
url = url.replaceFirst("\\btoken=.*?(&|$)", "token=new_value$1");
System.out.println(url);
url = "param1=value&token=1234235asdjaklj231k209a";
url = url.replaceFirst("\\btoken=.*?(&|$)", "token=new_value$1");
System.out.println(url);
Edge cases to consider are first that your token
may be the last parameter in the query string. To cover this case, we should check for token=...
ending in either an ambersand &
or the end of the string. But if we don't use a lookahead, and instead consume that ambersand, we have to also add it back in the replacement. The other edge case, correctly caught by @DodgyCodeException in his comment below, is that there be another query parameter which just happens to end in token
. To make sure we are really matching our token
parameter, we can preface it with a word boundary in the regex, i.e. use \btoken=...
to refer to it.
Output:
token=new_value&name=sam&firname=Mahan
param1=value&token=new_value

- 502,043
- 27
- 286
- 360
-
What if the query params were "thisisnotatoken=gotcha&token=1234abcd&..." – DodgyCodeException Sep 19 '17 at 14:37
-
@DodgyCodeException Then we should use a word boundary in this case. You certainly have a nose for dodgy code :-) – Tim Biegeleisen Sep 19 '17 at 14:40
-
thanks it worked for my cases and that case mentioned above by @DodgyCodeException is not gonna happen for me , but thanks for mentioning that – Mahan Sep 19 '17 at 14:46
-
1I'm glad it helped in your specific case, but for general use this is flaky - e.g. there may be a `&` somewhere which will get mistaken by the above. The best way to tackle it would be to use a well-tested URL params parsing library (such as Apache URLEncodedUtils - not saying it's well-tested as after all Apache is a so named because it's a bit patchy, but it's the only one I could find). Then use that to split into a list of name-value pairs, change the appropriate value, and re-encode into a string. – DodgyCodeException Sep 19 '17 at 15:15
make a viewModel.
public class veiwModel(){ String token ; // and get and set for exmample }
then use Gson if u have a json text .
Gson gson = new Gson();
yourViewModel = gson.fronJson(jsonText , viewModel.class);
System.out.println(yourViewModel.getToken());

- 295
- 1
- 12