如何在不使用脚本的情况下将Ant属性转换为小写?
解决Ant属性转为小写的问题
方案1:使用Ant-contrib的<lowercase>任务
Ant-contrib提供了专门的大小写转换任务,比propertyregex更直接,适合单纯的小写转换需求:
<lowercase property="html.branding_lc" value="${html.branding}" />
当html.branding设为DEFAULT时,html.branding_lc会被正确赋值为default。
方案2:修正propertyregex的用法(若必须使用正则)
如果一定要通过propertyregex实现,需要利用其支持的函数调用特性,在替换逻辑中对捕获到的大写字母做转换:
<propertyregex property="html.branding_lc" input="${html.branding}" regexp="([A-Z])" replace="${toLowerCase(\1)}" global="true" override="true" />
关键要点:
- 用捕获组
([A-Z])匹配单个大写字母 - 替换时通过
toLowerCase(\1)将捕获到的内容转为小写 - 开启
global="true"确保替换所有匹配的大写字母
前置要求
确保已正确引入Ant-contrib的任务定义:
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
内容的提问来源于stack exchange,提问作者Matthew Stern
相关产品推荐
相关产品推荐

