開頭的文件文本我想製作一個ant目標,它可以替換不以定義模式開頭的文件(或更多)的內容。有沒有解決這個使用ant和replaceregexp?還是替代這個? 例子:Ant替換不以
- 我有了文本文件boo.txt: 「這是噓聲」。
- 此外我有文件foo.txt,其文本爲: 「just foo」。
我想更改foo.txt的到文本: 「這只是FOO」
開頭的文件文本我想製作一個ant目標,它可以替換不以定義模式開頭的文件(或更多)的內容。有沒有解決這個使用ant和replaceregexp?還是替代這個? 例子:Ant替換不以
我想更改foo.txt的到文本: 「這只是FOO」
大概做飯了捕捉一個合適的正則表達式的情況。
這一個似乎適用於你給的例子。
<property name="prefix" value="this " />
<replaceregexp flags="s" match="^(${prefix})?(.*)" replace="${prefix}\2">
<fileset dir="files" />
</replaceregexp>
files
目錄這裏的所有文件。this
,它存儲在一個屬性中,因爲我們需要在兩個地方提及它。flags="s"
設置可確保我們將文件視爲單個字符串用於匹配目的(而不是匹配每行)。\1
中 - 它被丟棄。\2
。\2
。你可能會認爲它一如既往添加前綴,但採取前綴的路程,如果它已經存在 ...除了replaceregexp
任務將不寫入文件後,除非如果從現有的不同。
這僅僅是一個局部的答案...我不知道,如果它的正確,但至少可以或許給你一些方向
爲此,你需要螞蟻的contrib
<property name="folder.name" value="some-folder-name" />
<property name="text.substitution" value="this " />
<target name="main">
<foreach target="target2" param="file_path">
<path>
<fileset dir="${folder.name}" casesensitive="no" />
</path>
</foreach>
</target>
<target name="target2">
<loadfile property="message" srcFile="${file_path}"/>
<!-- f message doesnt have text.substitution at the begging -->
<!-- not sure if this is correct regex -->
<propertyregex property="myprop" input="${message}"
regexp="([^b]+)" select="\0" casesensitive="false" />
<if>
<equals arg1="${myprop}" arg2="{text.substitution}" />
<then>
<concat destfile="foo.txt" append="true">${text.substitution} </concat>
</then>
</if>
</target>
哇,非常優雅的解決方案,謝謝!我發現了一些類似的東西,看看周圍,展望未來,但這更簡單! – Robert 2010-11-08 13:28:44
你有ideea如何逃避前綴屬性值內的正則表達式特殊字符? – Robert 2010-11-08 14:15:37