2013-03-22 54 views
0

我一整天都在這一塊上撞着我的頭。我有一個從運行shell命令捕獲的字符串,我需要解析該字符串並提取文件的路徑。我有什麼工作好,直到我試圖複製該文件...我認爲我正在使用的正則表達式在路徑的前面留下一個空白字符,這會導致以後各種麻煩。帶ANT的額外空格propertyregex

這裏的輸入字符串的一個示例: [回波] P4ant具有輸出= // Perforce的/ DIR1/DIR2/DIR3/dir4/dir5/DIR6/dir7 /p4ant/p4ant-2010.1.293250.jar#1 - C:\ DIR1 \ DIR2 \ DIR3 \ dir4 \ dir5 \ DIR6 \ dir7 \ dir8 \ dir9 \ p4ant \ p4ant-2010.1.293250.jar

當我分析它,我得到這個: [迴音] P4ant本地路徑= C:\ DIR1 \ DIR2 \ DIR3 \ dir4 \ dir5 \ DIR6 \ dir7 \ dir8 \ dir9 \ p4ant \ p4ant-2010.1.293250.jar

這看起來不錯,但有一個額外的空間存在。我似乎無法擺脫它。 下面是我使用的代碼:

  <propertyregex property="p4ant.local.path" 
        input="${p4ant.have.output}" 
        regexp="([^-][^ ]+?)$" 
        select="\0" 
        casesensitive="false" 
      /> 

我在尋找的「儀表板空間」的組合,並且不包括在輸出中縱橫馳騁,但仍然包括出於某種原因的空間。

我在這裏做錯了什麼? 謝謝!

回答

1
\-\s <-- dash, followed by a whitespace 
(.+?) <-- Anything, up to the next matching character 
\s <-- whitespace 
(.+)$ <-- Anything, up to the end 

所以:
regexp="\-\s(.+?)\s(.+)$"

然後串聯$ 1和$ 2(對不起,不知道這是如何工作的ANT)