您的位置:首页 > 其它

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

2014-10-24 11:18 405 查看
一、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这串数字
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ant