Apache Ant – Extract a string from property using regular expression

The Ant-Contrib library is a must have tool if you need to have more control on your build flow. Here is another example on using the propertyRegex task provided by Ant-Contrib to select or replace a string from an input.

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="Ant Property Regex" default="version" basedir=".">
  <taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
      <pathelement location="${basedir}/lib/ant-contrib.jar"/>
    </classpath>
  </taskdef>

  <target name="version">
    <propertyregex
      property="select.example"
      input="package.ABC.name"
      regexp="package\.([^\.]*)\.name"
      select="\0"
      casesensitive="false" />

    <echo message="${select.example}"/>

    <propertyregex
      property="replace.example"
      input="package.ABC.name"
      regexp="(package)\.[^\.]*\.(name)"
      replace="\1.DEF.\2"
      casesensitive="false" />

    <echo message="${replace.example}"/>      
  </target>
</project>

 

See what we have.
ant-contrib-propert-regex
 

In the above example, the token assigned to the select and replace attribute has the following meaning.

\0: The entire input
\1: The 1st grouping
\2: The 2nd grouping
...
\n: The nth grouping

 

Done =)

Reference: Ant-contrib Tasks: PropertyRegex

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s