开发者

ant string manipulation : extracting characters from a string

开发者 https://www.devze.com 2023-02-10 04:22 出处:网络
I have an ant property which has value of the type 1.0.0.123 I want to extract the val开发者_如何学Cue after the last dot, in this case that would be \'123\'.

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

I want to extract the val开发者_如何学Cue after the last dot, in this case that would be '123'. Which ant task should i use and how?


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


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" />


Ok i have found the answer myself and this is tested. you just gotta use a bit of javascript.

<target name="get build ctr">

    <script language="javascript">
        <![CDATA[

                // getting the value
                buildnumber = myproj.getProperty("build.number");
                index = buildnumber.lastIndexOf(".");
                counter = buildnumber.substring(index+1);
                myproj.setProperty("buildctr",counter);

            ]]>
    </script>

</target>


<propertyregex property="xtractedvalue" 
               input="${foobar}" 
               regexp="(.*)\.(.*)$" 
               select="\2" />


Here's a solution using flaka without scripting =

<project xmlns:fl="antlib:it.haefelinger.flaka">

 <property name="foobar" value="1.0.0.123"/>

  <target name="main">   
   <!-- simple echo -->
   <fl:echo>xtractedvalue => #{split('${foobar}','\.')[3]}</fl:echo>
   <!-- create property for further processing.. -->
   <fl:let>
    xtractedvalue := split('${foobar}','\.')[3]
   </fl:let>
   <echo>$${xtractedvalue} => ${xtractedvalue}</echo> 
  </target> 
</project>


I would also use the ant-contrib PropertyRegex task. The following snippet only works if the input string follows the convention you used in your description and you can also use it to extract the other numbers by changing the value entered in the select tag.

<propertyregex property="output"
          input="input"
          regexp="(\d)\.(\d)\.(\d)\.(\d{3})"
          select="\4" />

You can also use the issest task on the output string to print out an error message if the input string does not follow the convention, because the output property will not be set.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号