Quantcast
Viewing all articles
Browse latest Browse all 26

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.
Image may be NSFW.
Clik here to view.
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


Filed under: Ant Tagged: Ant, Ant-Contrib Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 26

Trending Articles