import java.io.*; 
import java.util.regex.*; 
public class test 

    public static void main(String[] args) 
    { 
        String str = "d:\\aaa\\bbb\\ccc\\ddd.exe ";
        String regex="[a-zA-Z]:(\\\\[^/|\\*?<>:\"]+)*\\\\"; 
        Matcher m=Pattern.compile(regex).matcher(str);
        if(m.find())
            System.out.println(m.group());
            
        }            
} 为什么要用这么复杂的String regex="[a-zA-Z]:(\\\\[^/|\\*?<>:\"]+)*\\\\"; 
而不用String regex="[a-zA-Z]:(\\\\[a-z]+)\\\\"; 

解决方案 »

  1.   

    那再问下,[^/|\\*? <>:\"]这里面这些东西就是建目录不允许的符号了,那么\\和\有什么区别啊。。我糊的那句号也不允许啊,怎么没有排掉。还有逗号,那么多,排得完吗
      

  2.   

    除了a-z 还有 A-Z还有0-9还有...所以他写上了不允许的,不过我还是认为设置允许的更安全
      

  3.   

    / | \ * ? < > :  "
    在windows下.这九种字符是不能用来命名文件名和目录名的.