views:

223

answers:

1

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.

A: 

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 ?!

Best Regards, Gilbert

Rebse
As I commented above, this works only if I control the definition of the `a`. That's fine in a toy example, but suppose instead that the property comes from somewhere else, like a property file contributed from elsewhere that hasn't escaped the dollars in this fashion. Suppose further that it can't escape them, because the properties are used from a shell script that would be confused by the escape character.
Joe Kearney
That's pretty close. To be pedantic, the case that this misses is where there's a `$` somewhere else in the property that we don't want to replace, so for correctness it would be nice to be able to do this entirely in the regex. But it'll do for my purposes. I completely agree, it's calling for trouble, unfortunately it's part of a file path of a file system I don't control, so it's there whether I like it or not! Thanks for looking into it.
Joe Kearney
"I'd like to get the pair xyz/def and xyz/ghi. Is this possible?"you got what you asked for. For other input | output you have to provide more details / anotherexample snippet
Rebse