0

I am trying to do a substring operation using antcontrib's propertyregex. Here is my original string:

COMPONENT:MYBuild:cat:cat

Im trying to extract MYBuild from the above string.

<propertyregex property="builderName"
     input="${componentLine}"
     regexp="(.*)COMPONENT:.*:"
     replace=""
     casesensitive="true"
     override="true"/>

But this is fetching me

 COMPONENT:MYBuild:cat:

Can someone please help?

David W.
  • 105,218
  • 39
  • 216
  • 337
user1384205
  • 1,231
  • 3
  • 20
  • 39

2 Answers2

0

You don't need a regex, with And addon Flaka it's straightforward :

<project xmlns:fl="antlib:it.haefelinger.flaka">
 <property name="componentLine" value="COMPONENT:MYBUILD:cat:cat"/>
  <fl:let>
   builderName := split('${componentLine}', ':')[1]
  </fl:let>
  <echo>$${builderName} => ${builderName}</echo>
</project>

output :

 [echo] ${builderName} => MYBUILD

Flaka has other functions too, if you need a regex, use replace()
See Flaka Manual and Examples for details.

Rebse
  • 10,307
  • 2
  • 38
  • 66
  • Hi @Rebse, When I use the flaka tags, im getting the following error on the if loop(line 196) that contains this string operation using flaka /afs/btv.ibm.com/data/aes/ANT_REG/build.xml:196: java.lang.UnsupportedClassVersi onError: it/haefelinger/flaka/dep/Select (Unsupported major.minor version 49.0) at java.lang.ClassLoader.defineClass0(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java(Compiled Code)) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java(Compiled Code))at java.net.URLClassLoader.defineClass(URLClassLoader.java(Compiled Code – user1384205 Aug 06 '12 at 06:49
  • you get the error "Unsupported major.minor version 49.0" because Flaka is compiled with java 1.5 and you're using an earlier java version, i.e. 1.4 - check with "java -version" in console; you should switch to an up-to-date java version, i.e. 1.6.0_33 or 1.7.0_05 also for security reasons – Rebse Aug 06 '12 at 17:16
0

One of the neat things about regular expressions is the use of [^x] which means any character besides x. This is a great thing when you have to thwart the greediness of regular expressions. For example, [^:]* means select all characters up to the next colon:

<propertyregex property="builderName"
     input="${componentLine}"
     regexp="[^:]*:([^:]*)"
     select="\1"
     override="true"/>

The first [^:]*: matches COMPONENT:. This says match the entire string that doesn't contain a colon, and the colon that follows it. (You see how this works?)

The next ([^:]*) matches MyBuild. It's similar to the first, except it doesn't have a colon, so the colon following MyBuild won't be included. It's surrounded by parentheses because I want to capture it.

Instead of the replace parameter, I use the select parameter which allows me to say that I want to replace the whole string with just the first capture group (what's in parentheses).

I haven't tested this, but it should work, or at least point you in the right direction.

David W.
  • 105,218
  • 39
  • 216
  • 337
  • Hi @David W. I am seeing a new problem while implementing this solution. Most often the search string is COMPONENT:MYBuild:cat:cat but there can be cases where the search string is COMPONENT:MYBuild:cat:cat:cat1:cat2. When I use the following in normal cases, it picks up the first four params, in other cases where there are more than 4 params, the entire sequence is selected. – user1384205 Aug 06 '12 at 06:46
  • I just tested the solution I gave you on `Test:one:two:three:four:five:six`. It picked `one` as it's suppose to. Are you saying that the one you want changes depending upon the number of items? – David W. Aug 06 '12 at 15:42
  • yes David. So now I have used ant flaka like the following. builderName ::= split('${componentLine}', ':')[1] Thanks for your replies. – user1384205 Sep 02 '12 at 15:03