论坛首页 入门技术论坛

Ant propertyregex无返回值的问题

浏览 2500 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-05-31  
<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,可以得知,

 

Attribute Description Required
property The name of the property to set. Yes.
override If the property is already set, should we change it's value. Can be true or false No. Defaults to false
input The input string to be processed Yes.
regexp The regular expression which is matched in the input string. Yes (can be specified in a<regexp> subelement).
select A 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
replace A regular expression substitition pattern, which will be used to replace the given regular expression in the input string. Yes, unless a select is specified
casesensitive Should the match be case sensitive No. default is "true".
global Should a replacement operation be performed on the entire string, rather than just the first occurance No. default is false.
defaultValue The 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>
 

 

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics