替换Ant属性中的字符

是否有一种简单的方法来获取属性的值,然后将其复制到替换了某些字符的另一个属性中?


说propA=This is a value。我想将其中的所有空格替换为下划线,结果为propB=This_is_a_value。


jeck猫
浏览 482回答 3
3回答

米脂

使用Ant Contrib的propertyregex任务。我想你要:<propertyregex property="propB"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;input="${propA}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;regexp=" "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;replace="_"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;global="true" />不幸的是,给出的例子并不清楚,但是值得尝试。您还应该检查如果没有任何下划线,会发生什么情况-您可能还需要使用该defaultValue选项。

胡子哥哥

如果您想要一个仅使用Ant内置组件的解决方案,请考虑以下事项:<target name="replace-spaces">&nbsp; &nbsp; <property name="propA" value="This is a value" />&nbsp; &nbsp; <echo message="${propA}" file="some.tmp.file" />&nbsp; &nbsp; <loadfile property="propB" srcFile="some.tmp.file">&nbsp; &nbsp; &nbsp; &nbsp; <filterchain>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tokenfilter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <replaceregex pattern=" " replace="_" flags="g"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tokenfilter>&nbsp; &nbsp; &nbsp; &nbsp; </filterchain>&nbsp; &nbsp; </loadfile>&nbsp; &nbsp; <echo message="$${propB} = &quot;${propB}&quot;" /></target>输出是 ${propB} = "This_is_a_value"
打开App,查看更多内容
随时随地看视频慕课网APP