2017-09-22 62 views
-2

我有一個文件,其中存儲了git push命令的輸出。它的輸出看起來像這樣,如何從groovy中的字符串獲取URL

Counting objects: 3, done. 
Delta compression using up to 8 threads. 
Compressing objects: 100% (2/2), done. 
Writing objects: 100% (3/3), 315 bytes | 315.00 KiB/s, done. 
Total 3 (delta 0), reused 0 (delta 0) 
To https://github.com/... 
    99d7..344 <branch> -> <branch> 
Branch newBranch set up to track remote branch newBranch from origin. 

我保存這個輸出在一個變量,現在我想做的事該變量的操作和只獲取URL一樣,

https://githhub.com/... 

這是一個詹金斯管道代碼。 謝謝。

回答

0

我敢肯定有使用詹金斯或Github上提供了一些API更好的辦法,但如果這是不可用的,如果你知道的格局將保持一致,我建議正則表達式

String foo = ""; 
    String bar = "Counting objects: 3, done.\nDelta compression using up to 8 threads.\nCompressing objects: 100% (2/2), done.\nWriting objects: 100% (3/3), 315 bytes | 315.00 KiB/s, done.\nTotal 3 (delta 0), reused 0 (delta 0)\nTo https://github.com/...\n 99d7..344 <branch> -> <branch>\nBranch newBranch set up to track remote branch newBranch from origin."; 
    Pattern p = Pattern.compile("^to\\s+(.*$)",Pattern.CASE_INSENSITIVE|Pattern.MULTILINE); 
    Matcher m = p.matcher(bar); 
    if(m.find()) { 
     foo = m.group(1); 
     System.out.println(m.group(1)); 
    } 
    System.out.println(foo); 
    Output: https://github.com/... 
相關問題