Replace dot in between slash i.e. '/./' with single slash '/'

Viewed 110

For this string dirPath,

String dirPath = "c:/create/a/dir/very/deep/inside/../././../../../dir/";

I want the output string to look like :

"c:/create/a/dir/very/deep/inside/../../../../dir/";

I used :

dirPath.replaceAll("/[.]/", "/");

but that gave :

c:/create/a/dir/very/deep/inside/.././../../../dir/
                                   ^^^ 

then, tried with one more replaceAll as:

dirPath.replaceAll("/[.]/", "/").replaceAll("/[.]/", "/");

and that worked!

My question is why couldn't one call achieve the same result? How to achieve it in simplest way?

P.S. Another regex that didn't work for me : .replaceAll("($|/)[.]/", "$1")

1 Answers
Related