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这串数字

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值