2014-09-04 53 views
0

我試圖從groovy中使用一些正則表達式函數從git倉庫中提取版本標籤。groovy正則表達式tokenize()。grep()函數沒有產生預期的答案

def command = "\"C:/Program Files (x86)/Git/bin/sh.exe\" --login -i -c \"git ls-remote [email protected]:myproject.git refs/tags/[vV]*?.*?.*?\"" 
def proc = command.execute() 
proc.waitFor() 

def str = proc.in.text 
def pattern = ~/^refs\/tags\/[vV][0-9]+\.[0-9]+\.[0-9]+$/ 
System.out.println("is everything a string? " + str.tokenize().every{ it instanceof String }) 
System.out.println("String: $str") 
System.out.println("split: ${str.split()}") 
System.out.println("contains refs/tags/V0.0.0?: ${str.split()*.trim().contains('refs/tags/V0.0.0')}") 
System.out.println("contains refs/tags/V0.0.1?: ${str.split()*.trim().contains('refs/tags/V0.0.1')}") 
System.out.println("grep: " + ['refs/tags/V0.0.0', 'refs/tags/V0.0.1'].grep(pattern)) 
System.out.println("split grep: " + str.split().grep(pattern)) 
System.out.println("split findAll: " + str.split().findAll{ it =~ pattern }) 

而且我的輸出是這樣的:

String: SHA1asdf refs/tags/V0.0.0 
SHA1fdsa refs/tags/V0.0.0^{} 
SHA1hgfd refs/tags/V0.0.l 
SHA1dfgh refs/tags/V0.0.l^{} 

split: [SHA1asdf, refs/tags/V0.0.0, SHA1asdf, refs/tags/V0.0.0^{}, SHA1hgfd, refs/tags/V0.0.l, SHA1dfgh, refs/tags/V0.0.l^{}] 
contains refs/tags/V0.0.0?: true 
contains refs/tags/V0.0.1?: false 
grep: [refs/tags/V0.0.0, refs/tags/V0.0.1] 
split grep: [refs/tags/V0.0.0] 
split findAll: [refs/tags/V0.0.0] 

爲什麼不是令牌化()的grep()產生預期的答案?

我使用Groovy 2.3.6

更新:我添加了更多的測試結果,看到代碼和輸出

回答

2

不知道爲什麼,但你V0.0.1版本實際上是一個性格l而不是1

如果你只是運行下面的示例(只是改變了str),grep的將工作得很好:

def str = """ 
SHA1asdf refs/tags/V0.0.0 
SHA1fdsa refs/tags/V0.0.0^{} 
SHA1hgfd refs/tags/V0.0.1 
SHA1dfgh refs/tags/V0.0.1^{} 
""" 

def pattern = ~/^refs\/tags\/[vV][0-9]+\.[0-9]+\.[0-9]+$/ 
System.out.println("String: $str") 
System.out.println("tokenize: ${str.tokenize()}") 
System.out.println("grep: " + ["refs/tags/V0.0.0", "refs/tags/V0.0.1"].grep(pattern)) 
System.out.println("tokenize grep: " + str.tokenize().grep(pattern)) 

產生

grep: [refs/tags/V0.0.0, refs/tags/V0.0.1] 
tokenize grep: [refs/tags/V0.0.0, refs/tags/V0.0.1] 
+0

呼,我想常規被打破。原來,我只是一個壞typer:P – coderatchet 2014-09-04 06:40:31

+1

它發生,繼續開槽:) – kunal 2014-09-04 06:42:24