如何通过正则表达式获取目录名称?

时间:2019-08-08 15:18:02

标签: windows groovy directory jenkins-pipeline

我需要获取目录的路径或名称,这是我所知道的: -目录所在的文件夹 -其名称遵循给定的正则表达式

我该怎么做?

致力于Jenkins管道(常规)

2 个答案:

答案 0 :(得分:0)

您可以应用pwd管道步骤将当前目录作为String返回,并使用正则表达式组从中获取名称。

请参见以下示例,使用groovy从正则表达式中提取值:https://gist.github.com/EwanDawson/2407215

答案 1 :(得分:0)

我最终将powershell的结果分配给了变量,我通过告诉powershell步骤它具有一个stdout返回值来做到这一点,例如:

def folder = powershell (returnStdout: true, script: """
            Get-ChildItem -directory | Select-String -pattern '<pattern>'
            """)

与pwd()结合使用可以提供完整路径,尽管您也可以使用命令中的管道来获取完整路径,例如Get-ChildItem -directory | Select-Object Fullname | Select-String -pattern '<pattern>'