簡體   English   中英

groovy if語句與變量中的正則表達式

[英]groovy if statement with regex in variable

我想刪除以AAA開頭的Jenkins作業列表(例如)

當我用顯式字符串做它時它工作正常

if (item.name ==~ /AAA.*/){  
    item.delete()
 }

但是當我試圖從Jenkins Build屬性中獲取正則表達式時,我失敗了並且沒有調用if ...

import hudson.model.*
import hudson.node_monitors.*
import hudson.slaves.*
import java.util.concurrent.*

jenkins = Hudson.instance

def thr = Thread.currentThread()

def build = thr?.executable

String REGEX = build.environment.get("Include_Regex")

println "REGEX is " + REGEX

for (item in jenkins.items){
   println("\t ${item.name}");
   println("\t ${item.name.getClass()}");
   if (item.name ==~ REGEX){  
       item.delete()
       println("TEST");
    }

} 

當我運行它時,我得到所有作業的列表:在工作區中構建/ var / lib / jenkins / jobs / Bulk_Delete_Job / workspace

REGEX is /AAA.*/
     AAA_test_1
     class java.lang.String
     AAA_test_2
     class java.lang.String
     AAA_test_3
     class java.lang.String
     Bulk_Delete_Job
     class java.lang.String
     ConfiguredJob
     class java.lang.String
     JobZ
     class java.lang.String
     TEST_SCM
     class java.lang.String

但沒有刪除/沒有“TEST”打印 - > if if never true true ....

需要幫助,謝謝!

String不應包含斜杠 - 使用AAA。*而不是/AAA.*/。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM