文件java.util.regex.PatternSyntaxException:索引5附近的转义序列非法/不受支持

文件java.util.regex.PatternSyntaxException:索引5附近的转义序列非法/不受支持,java,regex,file,Java,Regex,File,你好,我想复制没有单打文件或文件夹的文件 当我运行这个方法时,我得到了一个堆栈 public static void copyFiles() { String path = "C:\\luna\\trunk\\"; List<File> files = (List<File>) FileUtils.listFiles(new File(path), new SuffixFileFilter("-4.7-SNAPSHOT.jar

你好,我想复制没有单打文件或文件夹的文件

当我运行这个方法时,我得到了一个堆栈

public static void copyFiles() {
    String path = "C:\\luna\\trunk\\";
    List<File> files = (List<File>) FileUtils.listFiles(new File(path), new SuffixFileFilter("-4.7-SNAPSHOT.jar"), TrueFileFilter.INSTANCE);
    Pattern pattern = Pattern.compile("^(C:\\luna\\trunk\\lunaRelease\\target\\lib\\)");
    for (File file : files) {
        Matcher matcher = pattern.matcher(file.getName());
        if (file.getName().matches("warsztaty-4.7-SNAPSHOT.jar") || file.getName().matches("lunaRelease-4.7-SNAPSHOT.jar") || matcher.find() ) {
            System.out.println("Don't copy this file");
        } else {
            System.out.println(file.getAbsolutePath());
            String source = file.getAbsolutePath();
            File sourcedir = new File(source);
            File dest = new File("C:\\library");
            try {
                FileUtils.copyFileToDirectory(sourcedir, dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
publicstaticvoidcopyfiles(){
String path=“C:\\luna\\trunk\\”;
List files=(List)FileUtils.listFiles(新文件(路径)、新后缀文件过滤器(“-4.7-SNAPSHOT.jar”)、TrueFileFilter.INSTANCE);
Pattern=Pattern.compile(“^(C:\\luna\\trunk\\lunaRelease\\target\\lib\”);
用于(文件:文件){
Matcher Matcher=pattern.Matcher(file.getName());
如果(file.getName().matches(“warsztaty-4.7-SNAPSHOT.jar”)| | file.getName().matches(“lunaRelease-4.7-SNAPSHOT.jar”)| | matcher.find()){
System.out.println(“不要复制此文件”);
}否则{
System.out.println(file.getAbsolutePath());
String source=file.getAbsolutePath();
File sourcedir=新文件(源);
File dest=新文件(“C:\\library”);
试一试{
copyFileToDirectory(sourcedir,dest);
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}
这是我运行方法时的堆栈

"C:\Program Files\Java\jdk1.8.0_251\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 5
^(C:\luna\trunk\lunaRelease\target\lib\)
     ^
    at java.util.regex.Pattern.error(Pattern.java:1969)
    at java.util.regex.Pattern.escape(Pattern.java:2485)
    at java.util.regex.Pattern.atom(Pattern.java:2212)
    at java.util.regex.Pattern.sequence(Pattern.java:2144)
    at java.util.regex.Pattern.expr(Pattern.java:2010)
    at java.util.regex.Pattern.group0(Pattern.java:2919)
    at java.util.regex.Pattern.sequence(Pattern.java:2065)
    at java.util.regex.Pattern.expr(Pattern.java:2010)
    at java.util.regex.Pattern.compile(Pattern.java:1702)
    at java.util.regex.Pattern.<init>(Pattern.java:1352)
    at java.util.regex.Pattern.compile(Pattern.java:1028)
    at CopyFiles.copyFiles(CopyFiles.java:23)
    at CopyFiles.main(CopyFiles.java:17)

Process finished with exit code 1
“C:\Program Files\Java\jdk1.8.0\U 251\bin\Java.exe”-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA线程“main”中的异常Java.util.regex.PatternSyntaxException:索引5附近的非法/不受支持的转义序列
^(C:\luna\trunk\lunaRelease\target\lib\)
^
位于java.util.regex.Pattern.error(Pattern.java:1969)
位于java.util.regex.Pattern.escape(Pattern.java:2485)
位于java.util.regex.Pattern.atom(Pattern.java:2212)
位于java.util.regex.Pattern.sequence(Pattern.java:2144)
位于java.util.regex.Pattern.expr(Pattern.java:2010)
位于java.util.regex.Pattern.group0(Pattern.java:2919)
位于java.util.regex.Pattern.sequence(Pattern.java:2065)
位于java.util.regex.Pattern.expr(Pattern.java:2010)
位于java.util.regex.Pattern.compile(Pattern.java:1702)
Pattern.(Pattern.java:1352)
位于java.util.regex.Pattern.compile(Pattern.java:1028)
在CopyFiles.CopyFiles(CopyFiles.java:23)
位于CopyFiles.main(CopyFiles.java:17)
进程已完成,退出代码为1

如何修复它?

新方法,因为我不想使用以下路径开始复制:C:\luna\trunk\lunaRelease\target\lib\”

publicstaticvoidcopyfiles(){
String path=“C:\\luna\\trunk\\”;
List files=(List)FileUtils.listFiles(新文件(路径)、新后缀文件过滤器(“-4.7-SNAPSHOT.jar”)、TrueFileFilter.INSTANCE);
用于(文件:文件){
如果(file.getName().matches(“warsztaty-4.7-SNAPSHOT.jar”)| | file.getName().matches(“lunaRelease-4.7-SNAPSHOT.jar”)| | file.getAbsolutePath().startsWith(“C:\\luna\\trunk\\lunaRelease\\target\\lib\”){
System.out.println(“不要复制此文件”);
}否则{
System.out.println(file.getAbsolutePath());
String source=file.getAbsolutePath();
File sourcedir=新文件(源);
File dest=新文件(“C:\\library”);
试一试{
copyFileToDirectory(sourcedir,dest);
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}

谢谢您的帮助。

尝试用\\\\\替换\\。好吧,错误消息没有错,正则表达式语法中没有特殊的序列,例如
\d
\w
\s
,因此
\
不应该是这样的序列的开始,所以应该转义。表示
\
的正则表达式可能看起来像
\
,可以用字符串文字编写为
“\\\\\”
。但是你为什么要用正则表达式呢?为什么
matches(“warsztaty-4.7-SNAPSHOT.jar”)
而不是
equals(“warsztaty-4.7-SNAPSHOT.jar”)
(与'lunaRelease-4.7-SNAPSHOT.jar'相同)。此外,如果您使用regex的唯一目标是使用它的行/数据起始点
^
锚,那么您可以使用
strData.startsWith(sequence)实现类似的效果
我解决了这样的问题:file.getAbsolutePath().startsWith(“C:\\luna\\trunk\\lunaRelease\\target\\lib\”)。注意:
matcher
也使用正则表达式,在正则表达式中,点
表示任何字符。matches(“warsztaty-4.7-SNAPSHOT.jar”)也将为
返回true。matches(“warsztaty-4#7-SNAPSHOT#jar”)
。要检查精确值,请使用
等于
而不是
匹配
public static void copyFiles() {
    String path = "C:\\luna\\trunk\\";
    List<File> files = (List<File>) FileUtils.listFiles(new File(path), new SuffixFileFilter("-4.7-SNAPSHOT.jar"), TrueFileFilter.INSTANCE);
    for (File file : files) {
        if (file.getName().matches("warsztaty-4.7-SNAPSHOT.jar") || file.getName().matches("lunaRelease-4.7-SNAPSHOT.jar") || file.getAbsolutePath().startsWith("C:\\luna\\trunk\\lunaRelease\\target\\lib\\") ) {
            System.out.println("Don't copy this file");
        } else {
            System.out.println(file.getAbsolutePath());
            String source = file.getAbsolutePath();
            File sourcedir = new File(source);
            File dest = new File("C:\\library");
            try {
                FileUtils.copyFileToDirectory(sourcedir, dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}