为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕尼黑8549860 2023-02-23 10:49:27
我目前有一个脚本,通过正则表达式包含和排除将文件从源目录复制到目标目录,但是当路径包含括号时,文件将不会复制。我最初的想法是,问题在于如何读取源和目标,因为 ( 是一个特殊字符,为了对抗我试图用转义的 (,但我可能做错了那部分。import groovy.io.FileTypeimport 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 += "/"        }我收到的错误要么是意外字符,要么是文件没有错误地移动。
查看完整描述

1 回答

?
慕姐8265434

TA贡献1813条经验 获得超2个赞

if(SourceFilePath.matches(IncludesRegexInfo.getAllRegexes() && !SourceFilePath.matches(ExcludesRegexInfo.getAllRegexes()))为了解决脚本不复制的问题, “匹配”两个字符串时出现问题。

字符串读取为相同的行但是在正则表达式方面不匹配。要解决此问题,您必须(转义. 这是用一个)ExcludesRegexInfo.getAllRegexes().replaceAll("\\(","\\\\(").replaceAll("\\)","\\\\)")


查看完整回答
反对 回复 2023-02-23
  • 1 回答
  • 0 关注
  • 80 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信