Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ant string manipulation : extracting characters from a string

Tags:

java

regex

ant

I have an ant property which has value of the type 1.0.0.123

I want to extract the value after the last dot, in this case that would be '123'. Which ant task should i use and how?

like image 622
pdeva Avatar asked Feb 10 '11 08:02

pdeva


2 Answers

With native ant task

If not wanting to use external libs nor scripting, I found in an answer to a similar question the best option (credit him for this answer). Here you'll be using a ReplaceRegex:

<loadresource property="index">
  <propertyresource name="build.number"/>
  <filterchain>
    <tokenfilter>
      <filetokenizer/>
      <replaceregex pattern=".*\.)" replace="" />
    </tokenfilter>
  </filterchain>
</loadresource>

(I have used the same variable names as you in your solution. Of course, this is still missing the increment part of your answer, but that was not in your question.)

This script loads in index the result of deleting the regex .*\.) from build.number, that is, if build.number = 1.0.0.123 then index = 123.


Example

build.xml:

<project name="ParseBuildNumber" default="parse" basedir=".">
    <property name="build.number" value="1.0.0.123"/>

    <target name="parse">
        <loadresource property="index">
            <propertyresource name="build.number"/>
            <filterchain>
                <tokenfilter>
                    <filetokenizer/>
                    <replaceregex pattern=".*\." replace="" />
                </tokenfilter>
            </filterchain>
        </loadresource> 
        <echo message="build.number=${build.number}; index=${index}"/>
    </target>

</project>
$ ant
Buildfile: /tmp/build.xml

parse:
     [echo] build.number=1.0.0.123; index=123

BUILD SUCCESSFUL
Total time: 0 seconds
like image 63
Alberto Avatar answered Sep 30 '22 12:09

Alberto


I suspect the easiest approach may be to use the ant-contrib PropertyRegex task.

Something like this - completely untested:

<propertyregex property="output"
          input="input"
          regexp=".*([^\.]*)"
          select="\1" />
like image 43
Jon Skeet Avatar answered Sep 30 '22 14:09

Jon Skeet