Ant propertyregex无返回值的问题

<propertyregex property="java.temppkg.dir" input="${base.package.path}/${lower.model.name}" regexp="\." replace="\/" >
</propertyregex>
<echo>${java.temppkg.dir}</echo>

 

如上面的ant代码,这段代码预计的作用是将input中的路径中带有"."的地方都替换成"/",然后将转换后的值赋给java.temppkg.dir.

 

实际的情况是,当input里有“.”的时候,java.temppkg.dir可以正常打印出值,而当input不带有"."的时候,echo语句打印不出值。

 

查看ant的api,可以得知,

 

AttributeDescriptionRequired
propertyThe name of the property to set.Yes.
overrideIf the property is already set, should we change it's value. Can be true or false No. Defaults to false
inputThe input string to be processedYes.
regexpThe regular expression which is matched in the input string.Yes (can be specified in a<regexp> subelement).
selectA pattern which indicates what selection pattern you want in the returned value. This uses the substitution pattern syntax to indicate where to insert groupings created as a result of the regular expression match.Yes, unless a replace is specified
replaceA regular expression substitition pattern, which will be used to replace the given regular expression in the input string.Yes, unless a select is specified
casesensitiveShould the match be case sensitiveNo. default is "true".
globalShould a replacement operation be performed on the entire string, rather than just the first occuranceNo. default is false.
defaultValueThe value to set the output property to, if the input string does not match the specific regular expression.No.

 

 

可以注意到,propertyregex有一个defaultValue的属性,这个属性的作用是,当没有匹配到要替换的值时,方法返回的默认值是

什么。那么就是说,如果没有这个默认值,当匹配不到需要替换的值时,propertyregex实际上不返回任何东西。因此,

上面的方法在Input没有"."将不会返回任何东西。这不符合java的思想,可能是因为propertyregex本身并不是为了字符替换而产生的。

综上,将代码修改为下面的样子就可以正常得到结果:

 

 

<propertyregex property="java.temppkg.dir" input="${base.package.path}/${lower.model.name}" regexp="\." replace="\/" defaultvalue="${base.package.path}/${lower.model.name}">
</propertyregex>
<echo>${java.temppkg.dir}</echo>
 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值