简体   繁体   English

在 Groovy 中使用带有闭包的 replaceAll

[英]Using replaceAll with a closure in Groovy

I have some Groovy code (actually from a Jenkinsfile):我有一些 Groovy 代码(实际上来自 Jenkinsfile):

           fileNew = fileOld.replaceAll("((D:)((.*)\\\\(.*))*)") {
                    it[0].replaceAll("D:", "/xxx/yyyy")
                    it[0].replaceAll("\\\\", "/")
            }
            writeFile file: 'my-output-file', text: fileNew

The code correctly matches the lines marked by the pattern in fileOld.replaceAll (as shown if I println it[0] ) but there is no replacement in the written file.代码正确匹配fileOld.replaceAll中模式标记的行(如我println it[0]所示),但写入文件中没有替换。 How can I get that to work?我怎样才能让它工作?

From what I'm seeing in the docs here it wouldn't work to call replaceAll inside of the replaceAll closure.从我在此处的文档中看到的内容来看,在replaceAll闭包内调用replaceAll是行不通的。 I think maybe you just need to do two separate replaceAll calls, searching for the "D:" and "\\\\" separately.我想也许你只需要做两个单独的replaceAll调用,分别搜索"D:""\\\\"

I was messing around with some different things and maybe using the original replaceAll would work.我在搞乱一些不同的事情,也许使用原始的replaceAll会起作用。

fileNew = fileOld.replaceAll("((D:)((.*)\\\\(.*))*)") {
     it[0] = it[0].replace("D:", "/xxx/yyyy")
     it[0].replace("\\\\", "/")
}

That worked for me but I wasn't positive what fileOld would look like so I had to guess a little there.这对我有用,但我fileOld会是什么样子,所以我不得不在那里猜测一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM