Groovy/Java - 复制文件路径中带有括号的问题

我目前有一个脚本,通过正则表达式包含和排除将文件从源目录复制到目标目录,但是当路径包含括号时,文件将不会复制。


我最初的想法是,问题在于如何读取源和目标,因为 ( 是一个特殊字符,为了对抗我试图用转义的 (,但我可能做错了那部分。


import groovy.io.FileType

import java.nio.file.*


    String Source = 'C:/temp/file(s)'

    String Target = 'C:/newTemp/file(s)'


    String InclusionsRegexes = "garbage.txt"


    String ExclusionsRegexes = ""


    class RegexInfo

    {

        private String AllRegexes = "";


        public RegexInfo(String RegexString, String RegexType, String Source)

        {

            if(RegexString != null)

            {

                def RegexArray = RegexString.split(",");


                for(item in RegexArray)

                {

                    String fullRegexPath = Source + "/" + item;


                    if(AllRegexes != null && !AllRegexes.isAllWhitespace())

                    {

                        //Add regex value for Or

                        AllRegexes += "|";

                    }

                    AllRegexes += fullRegexPath;

                }

            }

        }

        public String getAllRegexes() { return this.AllRegexes; }

    }


    IncludesRegexInfo = new RegexInfo(InclusionsRegexes, "inclusion", Source);

    ExcludesRegexInfo = new RegexInfo(ExclusionsRegexes, "exclusion", Source);


    File SourceDirToCopy = new File(Source);


    SourceDirToCopy.eachFileRecurse()

    {

        SourceFile ->


        String SourceFilePath = SourceFile.toString().replaceAll("\\\\","/");


        if(SourceFile.isDirectory())

        {

            SourceFilePath += "/"

        }


我收到的错误要么是意外字符,要么是文件没有错误地移动。


慕尼黑8549860
浏览 81回答 1
1回答

慕姐8265434

if(SourceFilePath.matches(IncludesRegexInfo.getAllRegexes() && !SourceFilePath.matches(ExcludesRegexInfo.getAllRegexes()))为了解决脚本不复制的问题, “匹配”两个字符串时出现问题。字符串读取为相同的行但是在正则表达式方面不匹配。要解决此问题,您必须(转义. 这是用一个)ExcludesRegexInfo.getAllRegexes().replaceAll("\\(","\\\\(").replaceAll("\\)","\\\\)")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java