Java string.replaceFirst / string.replaceLast regexp to remove everything after the second braid from the end

I have a path like this / my / long / path / which is / will / never / ends /. I would like to remove everything after the second slash from the end (so the output would be something like / my / long / path / which / will / never /). How could I do this? I can only use javas String.replaceFirst or String.replaceAll.

thank

+3


source to share


1 answer


You can use:

String str = "/my/long/path/which/will/never/ends/";
str = str.replaceFirst("[^/]+/$", "");
//=> /my/long/path/which/will/never/

      



Demo version of RegEx

+4


source







All Articles