1

i have a url like: http://example.com:8080/files/username/oldpassword/12351.png

i need to replace oldpassword with: new password.

oldpassword is not fixed string, it's unknown string.

currently i use this code:

String url = "http://example.com:8080/files/username/oldpassword/12351.png";
String[] split = url.split("/");
String oldPass = split[5];
String newPass = "anyNewRandomPassword";
if( !oldPass.equals(newPass)) {
     url = url.replace(oldPass, newPass);
}

i think it can be done using regex.

any help s much appreciated.

4

2 Answers 2

2

Using regex

String out = url.replaceFirst("(.*/)(.*)(/[^/]*)", "$1" + newPass + "$3");
url = out;
Sign up to request clarification or add additional context in comments.

3 Comments

how can extract password form this link? String url = "example.com:8080/files/username/oldpassword/12351.png";
and sometimes the link String url = "example.com:8080/username/oldpassword/12351.png"; it has the username after the domain.
@MohammedAhmed, it works fine for me to replace the old password with the code in my answer for your two new examples as well.
1

I think the AntPathMatcher is pretty handy for this kind of task and created a Scratch file for you. Hope this helps!

import org.springframework.util.AntPathMatcher;

import java.util.Map;

class Scratch {
    public static void main(String[] args) {
        final String givenUrl = "http://example.com:8080/files/username/oldpassword/12351.png\"";

        AntPathMatcher antPathMatcher = new AntPathMatcher();

        System.out.println("Analyse url '" + givenUrl + "'");
        Map<String, String> stringStringMap = antPathMatcher.extractUriTemplateVariables("**/{username}/{password}/**.**", givenUrl);

        String username = stringStringMap.get("username");
        String oldPassword = stringStringMap.get("password");
        System.out.println("username '" + username + "' + oldPassword '" + oldPassword + "'");

        String newPassword = "myNewSuperSecurePassword";
        System.out.println("Replacing it with new password '" + newPassword + ' ');

        String resultUrl = "";
        if(!newPassword.equals(oldPassword)){
            System.out.println("PASSWORD REPLACEMENT: New Password != old password and will be replaced");
            resultUrl = givenUrl.replace(oldPassword, newPassword);
        }else {
            System.out.println("NO REPLACEMENT: New Password equals old password");
            resultUrl = givenUrl;
        }

        System.out.println("Result URL '" + resultUrl + "'");
    }
}

4 Comments

some times the String url = "example.com:8080/username/oldpassword/12351.png"; and String url = "example.com:8080/username/oldpassword/12351"; how can i extract the password?
Try this new call with a modified pattern: antPathMatcher.extractUriTemplateVariables("**/{username}/{password}/*", givenUrl); This works for me! :)
Did this new pattern help you?
i used: String url = "example.com:8080/files/user1/password123/12351.png"; String[] split = url.split("/"); System.out.println(split[split.length - 2]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.