简体   繁体   中英

java regex to extract the password from url path between the last 2 slashes

how can i extract the password form the following links using java code:

it's one link and comes each time with different structure. the password is always between the last 2 slashes.

http://example.com:8080/files/user1/password123/12351.png
http://example.com:8080/user1/password123/956253.png
http://example.com:8080/user1/password123/3652

i need to get the password: password123

currently i use:

String url = "http://example.com:8080/files/user1/password123/12351.png";
String[] split = url .split("/");
//i reverse the array
String[] temp = new String[split.length];
for(int i = split.length - 1; i > -1; i --){
        temp[split.length - i -1] = split[i];
}
split = temp;
String password = split[1];

but this depends on the index of array.

is there a regex code to do so?

regards

If the structure of your data is always like that and password is always the between the last 2 slashes you could just use split and use the length of the array minus 2.

Apart from verifying that the index exists etc, you might use:

String url = "http://example.com:8080/files/user1/password123/12351.png";
String[] split = url.split("/");        
System.out.println(split[split.length - 2]);

Demo

I'd recommend don't go with split, try a regex.

String url = "http://example.com:8080/files/user1/password123/12351.png";
String pattern = ".*\\/([^\\/]+)\\/[\\w\\.]+";
Pattern r = Pattern.compile(pattern);
String pwd = "";
Matcher m = r.matcher(line);
if (m.find()) {
    pwd = m.group(1); 
}
else {
    System.out.println("NO MATCH");
}

Regular expressions are a heavyweight solution, so I prefer to avoid using them unless they are needed.

Instead, you can just look for the last / character, then look for the / character before that one:

int lastSlash = url.lastIndexOf('/');
int secondToLastSlash = url.lastIndexOf('/', lastSlash - 1);
String password = url.substring(secondToLastSlash + 1, lastSlash);

password = URLDecoder.decode(password, "UTF-8");

The last line is needed because the password might contain characters which cannot be directly present in a URL. For instance, the password sword{fish} would occur in a URL as sword%7bfish%7d .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM