我想基本上试图"dir1/dir2/../dir3"
转换为"dir1/dir3"
通过使用正则表达式如下:降低相对/规范路径的复杂性? (如何消除“..”?)
filePath.replaceAll("^(\\w)?(<>:\"/\\|?*)//\\.\\./", "");
我在做什么错在这里?
我知道有很多规范化规则,但我只想要应用两个规则:
All "." segments are removed.
If a ".." segment is preceded by a non-".." segment then both of these segments are removed. This step is repeated until it is no longer applicable.
我也试过:
result = filePath.replace("^(\\w+)\\.\\./", "");
但它并没有制定出任何
你确定File.getCanonicalPath()不是你真正需要的吗? –
如果您希望手动规范化并使用正则表达式,请考虑多次运行更简单的正则表达式(直到输出保持不变)。那么这只是用“”每个阶段替换“路径/ ..”(加边缘守卫)的问题。 – 2011-11-09 18:45:25
不..因为我不希望所有的规范化规则适用 – xonegirlz