I have the following string:
/home/user/Documents/something/
I'd like to remove the */ part at the end of it. In this case this is something/. How can I achive that with java?
I have the following string:
/home/user/Documents/something/
I'd like to remove the */ part at the end of it. In this case this is something/. How can I achive that with java?
You don't need regex to do this, below is a non-regex solution using String manipulation methods:
String s = "/home/user/Documents/something/";
String sub =s.substring(0, s.lastIndexOf("/"));
System.out.println(sub.substring(0,sub.lastIndexOf("/")+1));
You can use the following code. It will remove the last past from your String :
String str = "/home/user/Documents/something/";
Pattern pattern1 = Pattern.compile("(/.+?/)(\\w+?/$)");
Matcher matcher1 = pattern1.matcher(str);
while (matcher1.find()) {
System.out.println(matcher1.group(1));
}