views:

34

answers:

1

I have the following ant task:

<loadfile property="proj.version" srcfile="build.py">
    <filterchain>
        <striplinecomments>
            <comment value="#"/>
        </striplinecomments>
        <linecontains>
            <contains value="Version" />
        </linecontains>
    </filterchain>
</loadfile>
<echo message="${proj.version}" />

And the output is

 [echo] config ["Version"]          = "v1.0.10-r4.2"

How do I then use a tokenizer to get only v1.0.10-r4.2, the equivalent of

| cut -d'"' -f4
+2  A: 

you could use a containsregex element within your filterchain as tokenfilter.

This should filter any line with "Version" in it, and return as content the only group captured: 'vx.y.zz....'

<containsregex pattern="^.?\"Version\".\"(v[^\"]+?)\"." replace="\1" /> does not work.
May be <containsregex pattern='^.
?"Version"."(v[^\"]+?)".' replace="\1" /> could work (with single quotes for the parameter attribute, allowing for non-escaped double-quotes inside the parameter value)

Actually the OP John Oxley provides a working solution with the double quotes (\") replaced by &quot;:

<containsregex
        pattern="^.*?&quot;Version&quot;.*&quot;(v[^&quot;]+?)&quot;.*"
        replace="\1" />
VonC
Thank you. Almost worked. I had to use " to escape the " and then it worked perfectly.
John Oxley
@John: thank you for the feedback. Do you mean replace every double quotes by " as in: `pattern="^.*?\"Version\".*\"(v[^\"]+?)\".*"` ?
VonC
John Oxley
@John: thank you for this precision. I have update my answer to reflect the right pattern syntax.
VonC