ant修改文件中的內容 replace命令的使用

一、replace命令
<replacefilter token="@MIDLET@" value="${app.midlet}"/>
<replacefilter token="@JAR@" value="${app.project}"/>
<replacefilter token="@FILESIZE@" value="${size}"/>
<replacefilter token="@DESCRIPTION@" value="${app.description}"/> 
上面的replace命令用起來很簡單,但沒有辦法做正則匹配,如果要替換的字符串,是需要用正則來描述的,可以用replaceregexp
<replaceregexp byline="true"> 
<regexp pattern="[1-9][0-9]{4,}"/> 
<substitution expression="6356351"/> 
<fileset file="test.html"/> 
</replaceregexp> 
<replaceregexp byline="true">
<regexp pattern="[1-9][0-9]{4,}"/>
<substitution expression="6356351"/>
<fileset file="test.html"/>
byline="true" 表示替換所有滿足條件的字符串,若設爲false,則只會替換第一個滿足正則表達式的字符串
pattern="[1-9][0-9]{4,}"表示QQ號碼的正則表達式,當然,你也可以根據需求,替換成其他正則表達式
expression="6356351" 表示將滿足條件字符串替換成6356351
<fileset file="test.html"/> 表示在test.html這個文件中進行查找和替換,你也可以設置爲查找多個文件,具體方法請在網上搜索fileset 的相關配置

A.替換某一文件中的字符串

<property name="temp.newstring" value="hello world" />

<replace file ="base/testing.txt" token="@temp@" value="${temp.newstring}"  />

解釋:token是需要替換的標記;value是新值,將testing.txt文件中的@temp@替換爲hello world。

B.替換某個文件夾中存在特定標記(@CHARSET@)的文件

<property name="webapp.charset" value="UTF-8" />

<replace dir="temp" token="@CHARSET@" value="${webapp.charset}"/>

解釋:查詢temp目錄中的所有文件,將文件中存在@CHARSET@標記的內容替換爲${webapp.charset}變量值。

C.批量替換

<replace dir="dist" includes="${app.project}.jad" encoding="UTF-8">

<replacefilter token="@NAME@" value="${app.name}"/>

<replacefilter token="@VENDOR@" value="${app.vendor}"/>

<replacefilter token="@PRICE@" value="${app.price}"/>

</replace>


二、replaceregexp命令

例子:

</replaceregexp>

作用:將test.html中的QQ號碼替換成6356351這串數字

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章