escaping a dollar in the middle of an ant property
I have a property whose value contains a $
. I'd like to use this property as a regexp in a propertyregexp
. Ant appears to resolve the property as a paramater to the propertyregexp
, but then the dollar gets interpreted as a regexp symbol.
Example:
<property name="a" value="abc$" />
<property name="b" value="xyz" />
<path id="paths">
<pathelement location="abc$/def" />
<pathelement location="abc$/ghi" />
</path>
<pathconvert property="list" refid="paths" pathsep="${line.separator}" dirsep="/" />
<propertyregex property="list" input="${list}" override="true" regexp="${a}(.*)" replace="${b}\1" />
<echo message="${list}" />
I'd like to开发者_如何转开发 get the pair xyz/def
and xyz/ghi
. Is this possible? I'm using Ant 1.8.
oops somehow i didn't read your comment in all detail, but nevertheless, here's a working toy solution ;-)
<project name="project" default="main">
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
<property name="a" value="abc$" />
<property name="b" value="xyz" />
<path id="paths">
<pathelement location="abc$/def" />
<pathelement location="abc$/ghi" />
</path>
<target name="main">
<pathconvert property="list" refid="paths" pathsep="${line.separator}" dirsep="/" />
<propertyregex property="a" input="${a}" override="true" regexp="\$" replace="" />
<propertyregex property="list" input="${list}" override="true" regexp="\$" replace="" />
<propertyregex property="list" input="${list}" override="true" regexp="${a}" replace="${b}" />
<echo>${list}</echo>
</target>
</project>
result :
main:
[echo] /foobar/AntScripts/xyz/def
[echo] /foobar/AntScripts/xyz/ghi
BUILD SUCCESSFUL
IMO, using properties with '$' in it is calling for trouble, is there no other way ?!
Below code works:
<property name="a" value="abc$" />
<property name="b" value="xyz" />
<path id="paths">
<pathelement location="abc$/def" />
<pathelement location="abc$/ghi" />
</path>
<pathconvert property="list" refid="paths" pathsep="${line.separator}" dirsep="/" />
<propertyregex property="a.escaped" input="${a}" regexp="\$$" replace="\\\\$$" global="true" />
<propertyregex property="list" input="${list}" override="true" regexp="${a.escaped}(.*)" replace="${b}\1" />
<echo message="${list}" />
精彩评论