In Ant, I have a property named 'some_property
', and let's say its value is "hello
".
I'm trying to replace a place-holder inside a text file with this property's value ("hello") as an upper-case.
So, I have this task:
<replaceregexp match="SOME_PLACE_HOLDER" replace="${some_property}" byline="true">
And I want it to work as if I would have this task:
<replaceregexp match="SOME_PLACE_HOLDER" replace="HELLO" byline="true">
I wish to avoid external Ant tasks (such as Ant-Contrib), therefore the solution needs to be a pure regex - it must be possible!
UPPERCASE, lowercase, and Capitalized.
Anyone knows the correct regexes?
I understand that you want to avoid Ant extensions, but the constraint that the solution be implemented using regex is a little tight - apologies if the following bends (breaks?) that rule too much.
Ant ships with a javascript engine these days, so anything that seems problematic to implement in Ant xml can usually be hidden away in a scriptdef
. Below are four that do case changing.
In your case, you would take your some_property
property and process it through the upper
script to get an uppercased version of the string to use in the replaceregexp
task.
<scriptdef language="javascript" name="upper">
<attribute name="string" />
<attribute name="to" />
project.setProperty( attributes.get( "to" ),
attributes.get( "string" ).toUpperCase() );
</scriptdef>
<scriptdef language="javascript" name="lower">
<attribute name="string" />
<attribute name="to" />
project.setProperty( attributes.get( "to" ),
attributes.get( "string" ).toLowerCase() );
</scriptdef>
<scriptdef language="javascript" name="ucfirst">
<attribute name="string" />
<attribute name="to" />
var the_string = attributes.get( "string" );
project.setProperty( attributes.get( "to" ),
the_string.substr(0,1).toUpperCase() + the_string.substr(1) );
</scriptdef>
<scriptdef language="javascript" name="capitalize">
<attribute name="string" />
<attribute name="to" />
var s = new String( attributes.get( "string" ) );
project.setProperty( attributes.get( "to" ),
s.toLowerCase().replace( /^.|\s\S/g,
function(a) { return a.toUpperCase(); }) );
</scriptdef>
Example use:
<property name="phrase" value="the quick brown FOX jUmped oVer the laZy DOG" />
<upper string="${phrase}" to="upper" />
<lower string="${phrase}" to="lower" />
<ucfirst string="${phrase}" to="ucfirst" />
<capitalize string="${phrase}" to="capitalize" />
<echo message="upper( ${phrase} )${line.separator}= '${upper}'" />
<echo message="lower( ${phrase} )${line.separator}= '${lower}'" />
<echo message="ucfirst( ${phrase} )${line.separator}= '${ucfirst}'" />
<echo message="capitalize( ${phrase} )${line.separator}= '${capitalize}'" />
And output:
[echo] upper( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG'
[echo] lower( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'the quick brown fox jumped over the lazy dog'
[echo] ucfirst( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'The quick brown FOX jUmped oVer the laZy DOG'
[echo] capitalize( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'The Quick Brown Fox Jumped Over The Lazy Dog'
Thanks to Poni and Marco Demaio for the implementation of the Capitalization.