Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Ant property based on a regular expression in a file

Tags:

regex

ant

I have the following in a file

version: [0,1,0]

and I would like to set an Ant property to the string value 0.1.0.

The regular expression is

version:[[:space:]]\[([[:digit:]]),([[:digit:]]),([[:digit:]])\]

and I need to then set the property to

\1.\2.\3

to get

0.1.0

I can't workout how to use the Ant tasks together to do this.

I have Ant-contrib so can use those tasks.

like image 757
Matt Clarkson Avatar asked Dec 13 '22 08:12

Matt Clarkson


2 Answers

Based on matt's second solution, this worked for me for any (text) file, one line or not. It has no apache-contrib dependencies.

<loadfile property="version" srcfile="version.txt">
  <filterchain>
    <linecontainsregexp>
      <regexp pattern="version:[ \t]\[([0-9]),([0-9]),([0-9])\]"/>
    </linecontainsregexp>
    <replaceregex pattern="version:[ \t]\[([0-9]),([0-9]),([0-9])\]" replace="\1.\2.\3" />
  </filterchain>
</loadfile>  
like image 78
comodoro Avatar answered Jan 06 '23 22:01

comodoro


Solved it with this:

<loadfile property="burning-boots-js-lib-build.lib-version" srcfile="burning-boots.js"/>
<propertyregex property="burning-boots-js-lib-build.lib-version"
    override="true"
    input="${burning-boots-js-lib-build.lib-version}"
    regexp="version:[ \t]\[([0-9]),([0-9]),([0-9])\]"
    select="\1.\2.\3" />

But it seems a little wasteful - it loads the whole file into a property!

If anyone has any better suggestions please post :)

like image 24
Matt Clarkson Avatar answered Jan 06 '23 21:01

Matt Clarkson