2014-10-17 118 views
1

我有一個屬性,其中包含一個字符串,現在想要將其更改爲逗號分隔列表以測試文件中的行。用一個數值Ant LineContains逗號分隔值

目前以下工作:

<loadfile property="contents" srcFile="output.log"> 
    <filterchain> 
     <linecontains> 
      <contains value="${findvalue}"></contains> 
     </linecontains> 
    </filterchain> 
</loadfile> 

因此,如果在一個文件中的行包含:
的Hello World!

而findvalue ='World'的值會找到該行。現在我們要查找所有可能匹配多個單詞的行。所以如果一個文件的行包含:
Hello World!
再見!

我們希望設置屬性findvalue = World,Everyone並拾取文件的兩行。希望我對此有所理解,對於我來說完全解釋一點點困難。任何想法如何最好地完成這個?

回答

2

一種方法是使用linecontainsregexp過濾器來獲取匹配特定正則表達式的行。訣竅是將逗號分隔的值列表轉換爲單個正則表達式。

如果屬性findvalueWorld,Everyone,正則表達式可以簡單地World|Everyone,這意味着行包含任一WorldEveryone

<property name="findvalue" value="World,Everyone" /> 
<loadresource property="findvalueRegex"> 
    <propertyresource name="findvalue"/> 
    <filterchain> 
     <tokenfilter> 
      <filetokenizer/> 
      <replacestring from="," to="|" /> 
     </tokenfilter> 
    </filterchain> 
</loadresource> 

然後通過findvalueRegex屬性包含此正則表達式的linecontainsregexp過濾器:

<loadfile property="contents" srcFile="output.log"> 
    <filterchain> 
     <linecontainsregexp> 
      <regexp pattern="${findvalueRegex}" /> 
     </linecontainsregexp> 
    </filterchain> 
</loadfile> 
+0

非常感謝你這個完美! – bcoveny 2014-10-22 12:30:19