2016-07-18 39 views
1

我正在使用Jenkins並希望匹配名稱以「_TEST」結尾但無法通過正則表達式匹配的所有可構建作業。使用groovy匹配字符串的末尾

我已經嘗試了幾種方法,但一直沒能通過groovy來匹配它。

+1

你能添加你已經嘗試的代碼?它將幫助我們精確地回答你的問題。 –

+0

正則表達式還是Groovy?他們是如此不一樣! [正則表達式](https://en.wikipedia.org/wiki/Regular_expression)是進行字符串搜索/比較的表達式語法。 [Groovy](http://www.groovy-lang.org/)是一種完整的編程語言。 – Andreas

+0

我開始使用正則表達式在groovy腳本中進行匹配。後來我跑過了ensWith()函數,併爲我工作。 – Kithral

回答

1

等效的,但更簡潔的解決方案比this one

import hudson.model.* 

def list = Hudson.instance 
       .items 
       .findAll { it.buildable && it.name.endsWith("_TEST") } 
       .collect { it.name } 
+0

我喜歡這種簡單性。我怎麼才能讓它給我只是工作的名字?輸出如下所示:[email protected] [Build_Job_TEST] – Kithral

+0

我不編寫一個新的答案,我編輯了這個使用'collect',它將每個對象轉換成它的名字。格式化只是爲了說明Stack Overflow上的代碼而有些奇怪,並不一定是慣用的。 –

0

我實際上發現了一個不同的方式來解決我的問題。對不起,週一早晨:)

import hudson.model.* 

def list = [] 

for (item in Hudson.instance.items.findAll()) { 
    if (item.name.endsWith("_TEST")) { 
    if (item.isBuildable()) { 
     list.push(item.name) 
    } 
    } 
} 

return list 
+3

「groovy way」:'Hudson.instance.items.findAll {it.isBuildable()&& it.name.endsWith(「_ TEST」)}' –

相關問題