亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號